Is this possible??

Hello Experts,

 is it possible to run a process from a button on an applet? Basically, I have a server process for my chat system and it dies once and a while so I want the user to be able to start up the server by clicking on the
 button on the applet. My server is on a Unix system and to start it I run the command:

 >>java server

 I would really appreciate it if someone can tell me if this is possible and if so would you be kind enough to show me how. Thank you in advance!


 Marci Sarwan (marci_sarwan@yahoo.com)
marci_sarwanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
dini_dahmConnect With a Mentor Commented:
Marci,
Yeah this could be done. Look into
java.lang.Process process = Runtime.getRuntime().exec("should be you command to start the server");

Another suggestion is maybe look into a server-side piece of code which restarts the server if it goes down.
Having a client side button to do it is not really useful.

1) Bad security. You don't want the client restarting when it's up.
2) How are you going to talk to the server to tell it to restart? Your Java server is down and it's an
assumption to say that another one will still be up. Plus 2 java servers is insane.

Write something in a native script.

0
 
chrisosCommented:
You should be able to have another service, on another port waiting for a request to start the server, then you can execute a process by calling the following code:

public static int runIt() {
     int result = -1;
     try{
          Runtime runtime = Runtime.getRuntime();
          Process process = runtime.exec("java server");
     }
     catch( IOException ioe ) {
          ioe.printStackTrace();
     }
     catch( InterruptedException ie ) {
          ie.printStackTrace();
     }
     return result;
}

Obviously your button would send this request to the other port.

If you are interested I can even show you how to get the results from 'ps -ef | grep "java server"' and determine when to launch the app automatically!
0
 
chrisosCommented:
Ignore the return value, I forgot to cull that bit out of my code!
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
mgauravCommented:
One thing missing - you also need waitFor()

Master
0
 
marci_sarwanAuthor Commented:
Hello Chris,

thanks for replying.  Is this possible??

> If you are interested I can even show you how to get the results from 'ps -ef | grep "java >server"'  and determine when to launch the app automatically!

If this is possible maybe I can have the client, which is an applet, start the server if dies unexpectively. This would actually be better then what I have planned which was to have a button the client applet to start the server if it dies.   I would really appreciated if you can show me how?  Thanks so much!

Marci Sarwan (marci_sarwan@yahoo.com)
0
 
mgauravCommented:
One thing missing - you also need waitFor()

Master
0
 
marci_sarwanAuthor Commented:
Hello mqaurav,

where would I put the "waitFor()"? Thanks!

Marci Sarwan (marci_sarwan@yahoo.com)
0
 
chrisosCommented:
0
 
chrisosCommented:
Marci,

The code would change to:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("java server");
process.waitFor();
result = process.exitValue();

The result indicates the return value for the application.

Regards,

Chrisos
0
 
chrisosCommented:
0
 
chrisosCommented:
(Third time lucky!)

The following code allows you to run an external process and get the results.  The example code at the end is for a PC, but I'm sure you can determine where to make changes to the code for your own ends.

Regards,

Chrisos.

0
 
chrisosCommented:
0
 
chrisosCommented:
I give up!, I'll mail you the code!
0
 
objectsCommented:
Unless your applet is signed then you cannot run an external process using exec(), it'll throw a SecurityException.

Also is your server running on the same machine as the applet, or on the machine that the applet was loaded from?
If the latter, then using exec is no good to you anyway because it is running the process on the client machine.
What you need to do in that case is write a servlet, which the applet can call to perform the actions you require.

Hope this helps :)
0
 
marci_sarwanAuthor Commented:
Hello objects,

yes both the server and client are running on the Unix machine.  I never wrote a servlet before so would it be possible how I can go about doing this?  Thanks!


Marci Sarwan (marci_sarwan@yahoo.com)
0
 
marci_sarwanAuthor Commented:
Hello Christian,

I'm trying to test out your code but I'm having no luck.  Would it be possible if you can tell me what I need to do to test this code out?  Thank you!

Marci Sarwan (marci_sarwan@yahoo.com)
0
 
objectsCommented:
Theres a good tutorial on servlets here:
http://java.sun.com/docs/books/tutorial/servlets/index.html
0
 
marci_sarwanAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.