Solved

Is this possible??

Posted on 2001-06-06
18
268 Views
Last Modified: 2010-03-31
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)
0
Comment
Question by:marci_sarwan
  • 8
  • 5
  • 2
  • +2
18 Comments
 
LVL 3

Expert Comment

by:chrisos
ID: 6160029
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
 
LVL 3

Expert Comment

by:chrisos
ID: 6160033
Ignore the return value, I forgot to cull that bit out of my code!
0
 
LVL 2

Expert Comment

by:mgaurav
ID: 6160134
One thing missing - you also need waitFor()

Master
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:marci_sarwan
ID: 6160143
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
 
LVL 2

Expert Comment

by:mgaurav
ID: 6160145
One thing missing - you also need waitFor()

Master
0
 

Author Comment

by:marci_sarwan
ID: 6160324
Hello mqaurav,

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

Marci Sarwan (marci_sarwan@yahoo.com)
0
 
LVL 3

Expert Comment

by:chrisos
ID: 6160623
0
 
LVL 3

Expert Comment

by:chrisos
ID: 6160634
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
 
LVL 3

Expert Comment

by:chrisos
ID: 6160655
0
 
LVL 3

Expert Comment

by:chrisos
ID: 6160660
(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
 
LVL 3

Expert Comment

by:chrisos
ID: 6160662
0
 
LVL 3

Expert Comment

by:chrisos
ID: 6160665
I give up!, I'll mail you the code!
0
 
LVL 92

Expert Comment

by:objects
ID: 6161992
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
 

Author Comment

by:marci_sarwan
ID: 6162445
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
 

Author Comment

by:marci_sarwan
ID: 6162450
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
 
LVL 92

Expert Comment

by:objects
ID: 6162474
Theres a good tutorial on servlets here:
http://java.sun.com/docs/books/tutorial/servlets/index.html
0
 
LVL 1

Accepted Solution

by:
dini_dahm earned 200 total points
ID: 6671146
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
 

Author Comment

by:marci_sarwan
ID: 6671151
Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 84
Java DateChooser? 3 36
Arrays.asList  VS  ArrayList 4 56
MySQL  on Tomcat 8 45
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

770 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