Solved

Applet spawning a separate Java application ?

Posted on 1997-04-24
3
433 Views
Last Modified: 2013-12-29
Pedxing,
I need clarification, I put my further question in the "comments" section. After you answer this, I'll give you a really good grade. If you can't find the comment, please let me know...

I would like to have an applet spawn a separate Java process. I know how to create and use threads in Java, but I would like to have an applet spawn a completely separate application, interact with it via sockets, and then at some point kill the separate process.
I already know how to have separate processes communicate via sockets, I just don't know how to spawn a separate process and then kill it later.
Is such a thing possible in Java? It's okay if the applet and the process exist on the same machine. Sample code would be helpful (doesn't have to be elaborate)

Thanks in advance,
Dave

0
Comment
Question by:dgason
  • 2
3 Comments
 

Accepted Solution

by:
pedxing earned 100 total points
ID: 1219826
An applet cannot spawn an application on the same machine using standard security managers, as doing so would violate applet security and allow a rogue applet to have application-level control of your machine.  Not a good scenario!

I'm assuming here that your question refers to running a Java application, not another applet.  You can run multiple applets at the same time and if that is really what you want to do, let me know and I'll give you some code for that.

IF YOU ARE WILLING TO USE JDK 1.1.1

RMI (Remote Method Invocation) is the preferred method for doing this.  It requires the application to already be running (which is not precisely what you asked for).  It is also a very new API, which means that it won't be supported on a lot of browsers.  RMI programming is a big topic.  For information, take a look at:

http://www.javasoft.com:80/products/jdk/1.1/docs/guide/rmi/index.html

Pay particular attention to the tutorial, which gives you the code you were asking for.

IF YOU WANT THE APPLICATION ON THE SAME MACHINE (NO RMI):

Applet security can be relaxed in a couple of ways:

  a) replace the security manager in your browser. THIS IS DEFINITELY NOT RECOMMENDED!

  b) use a browser that allows you to configure the security options.  HotJava is the most flexible in this regard, I believe.  Again, this is a dangerous thing to do.

  c) More secure would be to use the Applet Signing system.  You can give permissions to signed applets to use system resources they wouldn't ordinarily be allowed to have.  Unfortunately, browsers which understand signed applets are in short supply right at the moment, so you lose portability.

However you get around the security restrictions, probably the easiest way to run the application would be to create a thread that had a call to java.lang.Runtime.exec().  It has the following forms:

  public class Runtime extends Object
  {
    ...
    public Process exec(String command) throws IOException;
    public Process exec(String command, String[] envp) throws IOException;
    public Process exec(String[] cmdarray) throws IOException;
    public Process exec(String[] cmdarray, String[] envp) throws IOException;
    ...
  }

IF THE APPLICATION CAN BE ON THE SERVER

Then you don't have a problem.  You can write an application that watches a particular port for messages, and a client that opens a socket to that port.  This works all the way back to JDK1.02.  Take a look at java.net.Socket.

Depending on the complexity and interactivity you wanted for the application, you could also store the application in the CGI-BIN directory and open a URL to it.  Look at java.net.URL, and possibly also java.net.URLConnection.

I hope this helps.
0
 

Expert Comment

by:pedxing
ID: 1219827
I'm sorry Dave, I'm afraid I can't do that.  (Sorry about that, I couldn't resist).

I don't see your comments.  You can try resubmitting them here, or email me: bruce@flair.law.ubc.ca.  I'll try to address any specific issues if I can.

And don't worry about the grade.  I'm answering your question in the hopes that someone will answer mine when I need it.  Points don't really interest me.  Thanks, though.

Bruce.
0
 

Author Comment

by:dgason
ID: 1219828
Hi!

I sent you an email with my additional questions. I hope that you can find a good answer!

Dave
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

808 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