Solved

applet is still running when i leave the page

Posted on 2003-11-08
54
501 Views
Last Modified: 2013-11-23
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
Comment
Question by:krozz56
  • 27
  • 21
  • 2
  • +2
54 Comments
 
LVL 1

Accepted Solution

by:
jdauie earned 100 total points
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 100 total points
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
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
 
LVL 5

Expert Comment

by:lwinkenb
Comment Utility
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
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
>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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> //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
 

Author Comment

by:krozz56
Comment Utility
yes... i dont now how, do you have a example ? or can you edit my last code?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
You cannot access member variables from a static method.
0
 

Author Comment

by:krozz56
Comment Utility
ok, is there a other way, to start a function in an other thread?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> that the function stopthis() in class InputToOutputb must be Static

Then you must not have posted the same code you are compiling.
0
 

Author Comment

by:krozz56
Comment Utility
well, my question is to call the function stopthis in the class INputToOUtputb. without stopthis MUST BE Static. Do you have an exapmle?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:krozz56
Comment Utility
-----------------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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> public InputToOutputb test;

You're calling stopthis() method in the InputToOutputb class, *not* the localserver class.
0
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> localserver.stopthis();

The variable name is init. It should be:

init.stopthis();
0
 

Author Comment

by:krozz56
Comment Utility
no, thats not right. It doesnt work. No errors, but the stopthis() will not run (tested with system.out.print("test")).
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:krozz56
Comment Utility
>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
 
LVL 92

Expert Comment

by:objects
Comment Utility
>  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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
no
0
 

Author Comment

by:krozz56
Comment Utility
ok.. why do i got the java.lang.NullPointerException error ?
0
 

Author Comment

by:krozz56
Comment Utility
-------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
 
LVL 92

Expert Comment

by:objects
Comment Utility
>  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
 

Author Comment

by:krozz56
Comment Utility
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
 

Author Comment

by:krozz56
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
no

I'm talking about the following init member var:

>     public localserver init;
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now