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

x
?
Solved

Using Runtime.exec() to call a Java class with arguments

Posted on 2003-10-26
11
Medium Priority
?
735 Views
Last Modified: 2008-02-01
I need to independantly run a Java class (with arguments) from a current running Java class using Runtime.exec().
 
I can use the Runtime.exec() with 100% success when calling .exe programs (e.g. Runtime.exec("start rmiregistry.exe")) but when wanting to start another java program with arguments (e.g. Runtime.exec("java -Djava.security.policy=client.policy ProductClient")) or even without arguments (e.g. Runtime.exec("start java ProductServer")), it will not work.
 
I am also unsure on how to call another class from a different directory than the working one? e.g. using something along the lines of Runtime.exec("c:\rmi\start java ProductServer") when the java program calling that was run from c:\rmi\client)
 
 
Any advice or knowledge would be greatly appreciated!!
0
Comment
Question by:John_Nash442
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 2

Accepted Solution

by:
f_98 earned 140 total points
ID: 9624670
you could try writing a batch file (or shell script) that sets up everything (arguments, directories) and then call that
batch file in runtime exec.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9625996
Runtime.exec("<full path to>java ProductServer");    MUST work!
You might also expand PATH with the path to all JAVA binaries, like javaw, javadoc ...
;JOOP!
0
 

Author Comment

by:John_Nash442
ID: 9626241
When using the line

Runtime.exec("c:\\rmi\\client\\server\\start java ProductServer");

It will compile but when running it i get the error message,

Error: java.io.IOException: CreateProcess: c:\rmi\client\server\start java ProductServer error=2

If this helps?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 24

Expert Comment

by:sciuriware
ID: 9626398
Type that same command in an MSDOS-box, see what happens.
(single \'s of course)

Did you try something like (valid on my computer):
E:\J2SDK1.4.2\BINjavaw -jar E:\j2sdk1.4.2\demo\jfc\SwingSet2\SwingSet2.jar
(depends on where your JDK is installed)
It runs a SWING-demo; if you can start that from an MSDOS-box,
try to Runtime.exec() it,
then apply your own program class name.
(step by step)
;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9626410
I think that your original command could work like:

Runtime.exec("c:/rmi/client/server/start   E:/J2SDK1.4.2/BIN/javaw   ProductServer");

(there was a missing \\ before BIN, then again: do not use \\ in your JAVA program:
   JAVA understands / very well.)
;JOOP!
0
 

Author Comment

by:John_Nash442
ID: 9626879
sciuriware, i've tried your suggestions but i still get the same error (the one I wrote up before).

When I write a batch file (something I don't have much experience in but I thought was pretty straight forward) it doesn't seem to recognise anything after the fisrt word, i.e. writing "c:\rmi\start java ProductServer" returns an error saying could not find "c:\rmi\start".
0
 
LVL 2

Expert Comment

by:f_98
ID: 9626946
try "start java c:\rmi\ProductServer" from the command line
0
 

Author Comment

by:John_Nash442
ID: 9627037
Okay sorry, I got my batch file running at the command line but Runtime.exec gives the same IOException error as I wrote up before.
0
 
LVL 2

Expert Comment

by:f_98
ID: 9627114
please post the error that it gives now. (the details or stack trace)
0
 

Author Comment

by:John_Nash442
ID: 9627430
Thanks for your help guys.

The Batch file was what I needed, just didn't know how to run one using runtime.exec().

For reference to anyone else looking at this:

( calling java classes in java using runtime.exec )

create a Batch file (just open up notepad, type in the lines and save it as a '.bat') with all your commands in them, mine looked like this:

cd\rmi\client\server
start rmiregistry
start java ProductServer
cd\rmi\client
start java ProductClient


For some reason I needed to have start in front of my java when trying to call another class. Don't know why but it works like this and makes no difference (to my purposes anyway).

Then I ran the batch file in my java program by using this code:

String[] cmd {"cmd", "/c", "nameOfBatFile.bat"};
Runtime rt = getRuntime();
Process p = rt.exec(cmd);
p.waitFor();

And it has called my Batch file.

This is all testing data for my real program so there may be some unforseen problems when implementing it. I'll post if i find anything. Hopefully this helps some people out because I found it hard to find straight forward information on this on the net.


0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9627435
Why did you first try : c:/rmi/client/server/start
and later                   c:\rmi\start

then:  MSDOS wants to see \ \ \ \
JAVA does not care if you use ///// or \\\\\\
but do not type //// to MSDOS.

And: did you verify that the files you are using do really exist on that place?

My program would be like this:

   String path = "<path to a program>";
   File f = new File(path);
   if(f.exists)
   {
      Runtime.exec(path + " <arguments>");
   }
   else
   {
      System.err.println("The file \"" + path + "\" does not exist.");
   }

;JOOP!
 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 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…
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.
Suggested Courses

715 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