Results 1 to 3 of 3
  1. #1
    stevek is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default 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:

    Java Code:
    java edu.lehigh.swat.bench.uba.Generator
    (edu is a directory inside of uba1.7/classes, lehigh is a directory inside of uba1.7/classes/edu)

    My first question is: why can I not run the command:

    Java Code:
    java classes.edu.lehigh.swat.bench.uba.Generator
    from the uba1.7 directory?



    My second question is this:

    I know I can use:

    Java Code:
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("java edu.lehigh.swat.bench.uba.Generator");
    in a Java program. But how do I get that command to execute from the specific directory it needs to be executed from?

    Thanks for your help

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Run "java" command from a java program

    Quote Originally Posted by stevek View Post
    I know I can use:

    Java Code:
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("java edu.lehigh.swat.bench.uba.Generator");
    in a Java program. But how do I get that command to execute from the specific directory it needs to be executed from?

    Thanks for your help
    Use a ProcessBuilder (see ProcessBuilder (Java Platform SE 6) ) and set its working directory as appropriate.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. Replies: 4
    Last Post: 09-11-2011, 05:21 AM
  3. Replies: 3
    Last Post: 11-08-2010, 03:23 AM
  4. Replies: 24
    Last Post: 06-21-2010, 04:49 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •