Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is this possible??

Posted on 2001-06-06
18
Medium Priority
?
279 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

704 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