Results 1 to 3 of 3
- 01-05-2012, 08:09 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
Run "java" command from a java program
I have a java program that can only be run by navigating to a specific directory (uba1.7/classes)
In this directory, I have to run the command:
(edu is a directory inside of uba1.7/classes, lehigh is a directory inside of uba1.7/classes/edu)Java Code:java edu.lehigh.swat.bench.uba.Generator
My first question is: why can I not run the command:
from the uba1.7 directory?Java Code:java classes.edu.lehigh.swat.bench.uba.Generator
My second question is this:
I know I can use:
in a Java program. But how do I get that command to execute from the specific directory it needs to be executed from?Java Code:Runtime rt = Runtime.getRuntime(); Process p = rt.exec("java edu.lehigh.swat.bench.uba.Generator");
Thanks for your help
- 01-05-2012, 09:14 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Run "java" command from a java program
Use a ProcessBuilder (see ProcessBuilder (Java Platform SE 6) ) and set its working directory as appropriate.
- 01-05-2012, 09:34 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Run "java" command from a java program
As regards your first question, the java command expects as an argument the name of a class. The name of a class and the name of a file are two different things, The reason you cannot say
classes.edu.lehigh.swat.bench.uba.Generator
is that classes is not a package. classes/edu/lehigh/swat/bench/uba/Generator.class may be a petfectly good file name, but there is no classes package.
What you can do is use an appropriate classpath. The classpath specifies the places where classes (including their packages) will be found. The classpath is specified using the -cp argument when you run the java executable. In the uba1.7 directory try
Java Code:java -cp classes edu.lehigh.swat.bench.uba.Generator
Similar Threads
-
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Why does my java command only work when I put "-cp ." before it?
By dunboody in forum New To JavaReplies: 4Last Post: 09-11-2011, 05:21 AM -
Help! Program cannot run. Error is "java.lang.NullPointerException".
By bMorgan in forum New To JavaReplies: 3Last Post: 11-08-2010, 03:23 AM -
how to write a program for " world cup " in java
By sus in forum New To JavaReplies: 24Last Post: 06-21-2010, 04:49 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks