Solved

Is this possible??

Posted on 2001-06-06
18
264 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now