[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

applet is still running when i leave the page

hi,

i have an applet running in a html page. the applet is a signed cab file. When i leaves the page, and come back, the java console sows this error:

java.lang.LinkageError: Class already defined in the context of this loader

and when i look if the port is open (its a applet with a serversocket) is it open. so, the applet is still running. when i leave the page.

how can i stop the applet when i leave the page?

note: i use <object> tags, not <applet> tags
0
krozz56
Asked:
krozz56
  • 27
  • 21
  • 2
  • +2
3 Solutions
 
jdauieCommented:
I think you just need to implement the stop() method in your applet class.  That is the method called by the browser when the page changes or the browser exits.  You should do any necessary housecleaning in that method.
0
 
krozz56Author Commented:
public class test extends Applet
{

public Thread thread3;

    public void main(String args[])
    {
    }

    public void init()
    {
        try
        {
            thread3 = new Thread(localser);
            thread3.start();
        }
        catch(Exception _ex) { }
    }


}
0
 
krozz56Author Commented:
ooops, i wasnt ready:

---------test.java-------------
public class test extends Applet
{

public Thread thread3;

    public void main(String args[])
    {
    }

    public void init()
    {
        try
        {  
            ...
            thread3 = new Thread(localser);
            thread3.start();
        }
        catch(Exception _ex) { }
    }


}
-------------localser.java-------------

class localserver extends Applet implements Runnable
{
  private String serv;
  private String port;
  public Thread threada, thread1;

    public localserver()
    {
    }

    public void run()
    {
        try
        {
            ...
            Thread threada = new Thread(inputtooutput);
            threada.start();
            Thread thread1 = new Thread(inputtooutputb);
            thread1.start();

        }
        catch(Exception _ex) { }
    }
}

--------------------------
my question. how can i with javascript, all the threads?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jimmackCommented:
In the run() method of each thread you presumably have a loop.  (eg. while (true)).

Create a flag that you can change to stop the thread.  (I'm guessing at your class format here ;-)) eg.

public class InputOutput implements Runnable
{
    private boolean running;

    public void run()
    {
        running = true;
        while (running)
        {
            .
            .
        }
    }

    public void stop()
    {
        running = false;
    }
}

Then from the stop() method in the applet, call the stop() method for each of your running threads.

public void stop()
{
    threada.stop();
    thread1.stop();
}
0
 
krozz56Author Commented:
ok,

it is not working. the loop is stopping, but when i leave the page and come back, the error:

ava.lang.LinkageError: Class already defined in the context of this loader

when i look to the thread list, is see that the thread threada and thread1 are still running. and the main thread also.
what's next what i can try?
0
 
jimmackCommented:
Can you confirm the individual threads are actually stopping as well as the main loop?

After calling stop() on the threads, you could try doing a join().  This will block until the thread has actually completed.

Then set the threada etc. to null as well.  This should mark them for garbage collection.
0
 
lwinkenbCommented:
Try verifying that your stop() method is getting called.  I'm not sure, but I think if your main thread is blocking (or in a main loop), stop() may never get called.
0
 
objectsCommented:
Its nothing to do with your threads.
It's to do with how you specify the class in the html tag:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q294878
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q270860

0
 
krozz56Author Commented:
ok, now when i come back at the page, there isnt a error.

but when i leaves the page the applet is still running. How can i stop the applet when i leave the page?
0
 
krozz56Author Commented:
now, i thought,
 i use the <BODY onUnload=stopmyapplet()>

that javascript runs the public void method stopit() in the applet.

---------- sub class (localserver.class) -----
    public void run()
    {
        try
        {
...
ServerSocket socketA = new ServerSocket(1234);
...

---------- main class (test.class) ------
public void stopit(){

            // now, what do i need to put here,
            // to stop ServerSocket in the thread ?

}
public void myapplet(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
----------
0
 
objectsCommented:
> How can i stop the applet when i leave the page?

The applet is automatically stopped (by calling the applets stop() method) when you leave the page. If you have any processing that needs to be stopped the stop it in the applets stop() method.

> that javascript runs the public void method stopit() in the applet.

You don't need to run any js.
0
 
krozz56Author Commented:
i have :


public void stopit(){
System.out.print("stopit");
stop();
}


when i run the javascript which call this function, Stopit will be print, but the applet still running. The port 1234 is still open. it needs to close. In the Thread thread3 is the socket. When i stop that socket, its allright. So, how can I talk with that thread? (my previous question)

regards jeffrey

0
 
objectsCommented:
You don't need to do that. The browser will automatically call stop(), no javascript is required.

You need to add a stop() method thats stops your thread(s) and closes your sockets.
Which implies you'll need to add the ability to stop your thread if you have not already.
0
 
krozz56Author Commented:
>You don't need to do that. The browser will automatically call stop(), no javascript is required.
ok

well, first i want to stop my sockets. the sockets are in the Thread3
how can i stop a socket from the main thread?
i thougth     thread3.serversocketB.close();  it won't works.

And after that i want to kill the thread3. How can i do that?
0
 
objectsCommented:
> ow can i stop a socket from the main thread?

you need a reference to the socket.

> And after that i want to kill the thread3. How can i do that?

Threads stop when the run() method completes.
The following tutorial includes examples of how to use threads.

http://java.sun.com/docs/books/tutorial/essential/threads/index.html
0
 
krozz56Author Commented:
ok, how can i make a refence to the socket. the socket is in the thread  thread3.thread7 and his name is SocketB

my other question, how can I define a String in a thread from the main thread? I cant find that on that site. Or how can i run a method in a other thread?

           localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();

the problem is that after the THREAD3.start(), i cant use localser.stopthreads().
is it possible with a other way?
0
 
objectsCommented:
> how can i make a refence to the socket.

You could add a method that returns it.
Or better still, one that closes it.

>  how can I define a String in a thread from the main thread?

Same way you define a String anywhere. They are just normal classes.

> is it possible with a other way?

You need to implement your own method to stop a thread, there is no standard way.
Read the tutorial I posted above for an example.

0
 
krozz56Author Commented:
i have 3 class files. number 1 starts 2 and 3 as a thread.
well. how can i talk to that threads, i want to start a function in that threads.

this is what i wanna try:

---------- sub class (localserver.class) -----
class InputToOutputb extends Applet implements Runnable{
private Socket socketa;
   public void stopthesocket(){
   socketA.close()
   }
    public void run()
    {
        try
        {
...
socketA = new ServerSocket(1234);
...

        }
    }
}
---------- main class (test.class) ------
Public Thread thread3
public void stopit(){

      //now, how can i start the function stopsocket() in the thread thread3

}
public void myapplet(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
----------
0
 
objectsCommented:
> //now, how can i start the function stopsocket() in the thread thread3

Just call the method thew same way you call any method.
This will require you maintain a reference to InputToOutputb.
0
 
krozz56Author Commented:
yes... i dont now how, do you have a example ? or can you edit my last code?
0
 
objectsCommented:
private InputToOutputb myapplet;

public void stopit(){

      //now, how can i start the function stopsocket() in the thread thread3

      myapplet.stopthesocket();
}

Though the thread should probably close it's own socket.
0
 
krozz56Author Commented:
yeah, thats works fine! I added Static to the function stopthesocket to let it works.

my !finally! question:

-----------------localserver
class localserver extends Applet implements Runnable
{
  private Socket socket1;
...
    public static void stopthis(){
      try{
           socket1.close();
      }catch(Exception _ex) { }
    }

    public void run()
    {
        try
        {
            ServerSocket serversocket = new ServerSocket(1234);
            Socket socket = serversocket.accept();
            socket1 = new Socket("123.234.345.456", 1234);
        }
        catch(Exception _ex) { }
    }
}
--------------
well, when the function stopthis() runs, he need to close the "Socket socket" and the "Socket socket1".  But my compileProgram returns a error to the line with "socket1.close();", the error is "cannot reference member 'socket1' without an object". What i am doing wrong?
0
 
objectsCommented:
You cannot access member variables from a static method.
0
 
krozz56Author Commented:
ok, is there a other way, to start a function in an other thread?
0
 
objectsCommented:
There is only one to start a function and that is to call it, if thats what you mean.
Which I have included an example of above.
0
 
krozz56Author Commented:
ok, look:

-----------------test
public class test extends Applet{
public InputToOutputb test;

public void stopit(){
test.stopthis();
}
public void myapplet(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
}
-----------------localserver
class localserver extends Applet implements Runnable
{
  private Socket socket1;
...
    public static void stopthis(){
      try{
           socket1.close();
      }catch(Exception _ex) { }
    }

    public void run()
    {
        try
        {
            ServerSocket serversocket = new ServerSocket(1234);
            Socket socket = serversocket.accept();
            socket1 = new Socket("123.234.345.456", 1234);
        }
        catch(Exception _ex) { }
    }
}
--------------

with this way, i cant close the socket, because the close function is in a Static public void. That must be Static, otherwise the Compiler gives the error "cannot make static call to non-static call".  How can i make a non-static call ?
0
 
objectsCommented:
> because the close function is in a Static public void

It shouldn't be static

> How can i make a non-static call ?

Same way you make any non-stsic class.
eg. from your code:
serversocket.accept();
socket1.close();
localser.startlocalserver(a);
thread3.start();
0
 
krozz56Author Commented:
with this method:

-----
...
public InputToOutputb test;

public void stopit(){

test.stopthis();
...
-----

gives the compiler the error, that the function stopthis() in class InputToOutputb must be Static.
but when that function is static, he cant call other function in that class. But he have to..

How can i make a call to the function stopthis in the class InputToOutputb from the class Test, without the function stopthis must be a Static (what the compiler says)
0
 
objectsCommented:
> that the function stopthis() in class InputToOutputb must be Static

Then you must not have posted the same code you are compiling.
0
 
krozz56Author Commented:
well, my question is to call the function stopthis in the class INputToOUtputb. without stopthis MUST BE Static. Do you have an exapmle?
0
 
objectsCommented:
> my question is to call the function stopthis in the class INputToOUtputb

Actually your question was why you were gett a LinkageError :)

> Do you have an exapmle?

The code you posted above should not get that error, please post the complete code you are compiling.
0
 
krozz56Author Commented:
-----------------test
public class test extends Applet{
public InputToOutputb test;

public void stopit(){
test.stopthis();
}
public void myapplet(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
}
-----------------localserver
class localserver extends Applet implements Runnable
{
  private Socket socket1;
...
    public void stopthis(){
      try{
           socket1.close();
      }catch(Exception _ex) { }
    }

    public void run()
    {
        try
        {
            ServerSocket serversocket = new ServerSocket(1234);
            Socket socket = serversocket.accept();
            socket1 = new Socket("123.234.345.456", 1234);
        }
        catch(Exception _ex) { }
    }
}
--------------

error: cannot make static call to non-static call  ( calling stopthis() from stopit() )
0
 
objectsCommented:
> public InputToOutputb test;

You're calling stopthis() method in the InputToOutputb class, *not* the localserver class.
0
 
krozz56Author Commented:
wooops. the same error ..
-----------------test
public class test extends Applet{
public localserver init;

public void stopit(){
localserver.stopthis();
}
public void init(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
}
-----------------localserver
class localserver extends Applet implements Runnable
{
  private Socket socket1;
...
    public void stopthis(){
      try{
           socket1.close();
      }catch(Exception _ex) { }
    }

    public void run()
    {
        try
        {
            ServerSocket serversocket = new ServerSocket(1234);
            Socket socket = serversocket.accept();
            socket1 = new Socket("123.234.345.456", 1234);
        }
        catch(Exception _ex) { }
    }
}
--------------

error: cannot make static call to non-static call  ( calling stopthis() from stopit() )
0
 
objectsCommented:
> localserver.stopthis();

The variable name is init. It should be:

init.stopthis();
0
 
krozz56Author Commented:
no, thats not right. It doesnt work. No errors, but the stopthis() will not run (tested with system.out.print("test")).
0
 
objectsCommented:
> no, thats not right

Thats how you call a method.
Do you use a different method to call a method?

> }catch(Exception _ex) { }

You shouldn't ignore exceptions

If thats the complete code you have posted then the problem is that you never set the init variable. I'd suggest passing it to the ctor and setting it then.
0
 
krozz56Author Commented:
>You shouldn't ignore exceptions
i tried with system.out.print() but there is no output.

>Thats how you call a method.
>Do you use a different method to call a method?
 you can see my method in the example.

-----------------code-------
public localserver INIT;

public void stopit(){
localserver.stopthis();
}
public void INIT(){

This is the way it works. but now stopthis in the thread must be static says the compiler. i dont want that.
0
 
objectsCommented:
>  i dont want that.

Thats why you have to call the method on the member variable and not the class as I already pointed out above.

If stopthis is not getting called and no exception is being thrown then it is probably because stopit() is not being called.

Do you actually set the init variable as I commented above?
0
 
krozz56Author Commented:
I had this:

---------- main class (test.class) ------
Public Thread thread3
public void stopit(){

      //now, how can i start the function stopsocket() in the thread thread3

}
public void myapplet(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
----------

You Said do this:

---------

private InputToOutputb myapplet;

public void stopit(){

      //now, how can i start the function stopsocket() in the thread thread3

      myapplet.stopthesocket();
}

--------------
I changed public void myapplet(){  to public void init(){, so i  changed MYAPPLET to INIT :
--------------
private InputToOutputb init;

public void stopit(){

      //now, how can i start the function stopsocket() in the thread thread3

      init.stopthesocket();
}
-------------
my completed script:
-------------
public class test extends Applet{
public localserver init;

public void stopit(){
localserver.stopthis();
}
public void init(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
}
-----------------localserver
class localserver extends Applet implements Runnable
{
  private Socket socket1;
...
    public STATIC   <-- (Must be says the compiler) -->  void stopthis(){
      try{
           socket1.close();
      }catch(Exception _ex) { }
    }

    public void run()
    {
        try
        {
            ServerSocket serversocket = new ServerSocket(1234);
            Socket socket = serversocket.accept();
            socket1 = new Socket("123.234.345.456", 1234);
        }
        catch(Exception _ex) { }
    }
}
--------------
0
 
objectsCommented:
One last time:

public class test extends Applet{
public localserver init;

public test(localserver s)
{
   init = s;
}

public void stopit(){
init.stopthis();
}
public void init(){
            localserver localser = new localserver();
            localser.startlocalserver(a);
            thread3 = new Thread(localser);
            thread3.start();
}
}
-----------------localserver
class localserver extends Applet implements Runnable
{
  private Socket socket1;
...
    public void stopthis(){
      try{
           socket1.close();
      }catch(Exception _ex) { }
    }

    public void run()
    {
        try
        {
            ServerSocket serversocket = new ServerSocket(1234);
            Socket socket = serversocket.accept();
            socket1 = new Socket("123.234.345.456", 1234);
        }
        catch(Exception _ex) { }
    }
}
0
 
krozz56Author Commented:
no, it doesn't work.  The compiler gives no error. But when i run the applet in my page, the applet dont starts. The OnError in the <object tag will start. The Java Console gives also no error. Whats the problem now ?
0
 
krozz56Author Commented:
public class test extends Applet
{
    public localserver init;
    public partypassion()
    {
    }

    public void main(String args[])
    {
    }

    public void stopit(){
    System.out.print("-stoppen-");
 try{
    init.stopthis();
}catch(Exception ex) {
System.out.print(ex);}
    }

--------
the exeption of this code is  java.lang.NullPointerException
0
 
objectsCommented:
> the exeption of this code is  java.lang.NullPointerException

You never define init (as I already mentiuoned twice), and I already showed you in the above code.
Why did you change the code that I posted??
0
 
krozz56Author Commented:
actually i used that threed, cause there is a blocking call,   serversocket.accept().
Can i with a other way ignore the blockingcall, that the script is going run en not waiting for a connection??
0
 
objectsCommented:
no
0
 
krozz56Author Commented:
ok.. why do i got the java.lang.NullPointerException error ?
0
 
krozz56Author Commented:
-------test.java---------
public class test extends Applet
{

    public localserver init;
    public Thread thread3;


    public test()
    {
    }

    public void stopit(){
try{
System.out.print("stop alles - ");
init.stopthis();
}catch(Exception ex) {
System.out.print("woops: "+ex);
}
    }

    public void init()
    {
        try
        {
 ...
            localserver localser = new localserver();
            localser.startlocalserver(s1a);
            thread3 = new Thread(localser);
            thread3.start();

-------localserver.java-------
class localserver extends Applet implements Runnable
{
  private String serv;
  private InputToOutput run;
  private Socket socket1, socket;

    public void startlocalserver(String s)
    {
   serv = s;
    }

    public localserver()
    {
    }
   
    public void stopthis(){

    try{
    System.out.print("-stoping numbers:-");
    }catch(Exception _ex) {System.out.print("-error:-"); }
    }


the compiler gave no error. but the java console will return: Woops: java.lang.NullPointerException

whats the problem. everthing is good, says the compiler
0
 
objectsCommented:
>  why do i got the java.lang.NullPointerException error ?

Because (for the 3rd time) you neve set the 'init' variable (as I specified in the earlier code).
ie. init is null.
0
 
krozz56Author Commented:
yes I do:

public localserver INIT;
....
   public void INIT()
    {
        try
        {
 ...
            localserver localser = new localserver();
            localser.startlocalserver(s1a);
            thread3 = new Thread(localser);
            thread3.start();


is it good like this ?
0
 
krozz56Author Commented:
yes I do:

public localserver INIT;
....
   public void INIT()
    {
        try
        {
 ...
            localserver localser = new localserver();
            localser.startlocalserver(s1a);
            thread3 = new Thread(localser);
            thread3.start();


is it good like this ?
0
 
objectsCommented:
no

I'm talking about the following init member var:

>     public localserver init;
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 27
  • 21
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now