Solved

Applet spawning a separate Java application ?

Posted on 1997-04-24
3
430 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

23 Experts available now in Live!

Get 1:1 Help Now