Solved

Is this possible??

Posted on 2001-06-06
18
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

623 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