Link to home
Start Free TrialLog in
Avatar of krozz56
krozz56

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of jdauie
jdauie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krozz56
krozz56

ASKER

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) { }
    }


}
Avatar of krozz56

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krozz56

ASKER

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?
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.
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.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krozz56

ASKER

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?
Avatar of krozz56

ASKER

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();
}
----------
> 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.
Avatar of krozz56

ASKER

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

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.
Avatar of krozz56

ASKER

>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?
> 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
Avatar of krozz56

ASKER

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?
> 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.

Avatar of krozz56

ASKER

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();
}
----------
> //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.
Avatar of krozz56

ASKER

yes... i dont now how, do you have a example ? or can you edit my last code?
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.
Avatar of krozz56

ASKER

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?
You cannot access member variables from a static method.
Avatar of krozz56

ASKER

ok, is there a other way, to start a function in an other thread?
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.
Avatar of krozz56

ASKER

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 ?
> 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();
Avatar of krozz56

ASKER

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)
> that the function stopthis() in class InputToOutputb must be Static

Then you must not have posted the same code you are compiling.
Avatar of krozz56

ASKER

well, my question is to call the function stopthis in the class INputToOUtputb. without stopthis MUST BE Static. Do you have an exapmle?
> 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.
Avatar of krozz56

ASKER

-----------------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() )
> public InputToOutputb test;

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

ASKER

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() )
> localserver.stopthis();

The variable name is init. It should be:

init.stopthis();
Avatar of krozz56

ASKER

no, thats not right. It doesnt work. No errors, but the stopthis() will not run (tested with system.out.print("test")).
> 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.
Avatar of krozz56

ASKER

>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.
>  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?
Avatar of krozz56

ASKER

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) { }
    }
}
--------------
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) { }
    }
}
Avatar of krozz56

ASKER

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 ?
Avatar of krozz56

ASKER

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
> 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??
Avatar of krozz56

ASKER

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??
Avatar of krozz56

ASKER

ok.. why do i got the java.lang.NullPointerException error ?
Avatar of krozz56

ASKER

-------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
>  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.
Avatar of krozz56

ASKER

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 ?
Avatar of krozz56

ASKER

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 ?
no

I'm talking about the following init member var:

>     public localserver init;