Results 1 to 3 of 3
  1. #1
    NaturalE is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Running Jar file using command line through Java Codes

    What i want to do is a program that calls the Command prompt and the command prompt would run a specific jar file... here is my code:
    Java Code:
    try {
          String[] commands = {"cd.."};
          Runtime.getRuntime().exec("cmd /c start", commands);
          }
          catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
    in the code above it opens up the cmd but fails to do the command "cd.."

    another thing i observed is when i try to manually input the command to run my specific jar file in the cmd [java -jar "location of file"] the cmd does not recognize "java" as a command...

    on contrary when i run the cmd through the start and input exactly same command the jar file successfully executes


    -----------------------------------------------------------------------
    SLIGHTLY OFF TOPIC YOU DONT NEED TO READ IF YOU DONT WANT TO

    At first what i wanted to do was to design a GUI which includes a cmd... but i failed so instead what i did was to call cmd from a program, problem is the system.out command and other commands that need the cmd to output doesnt appear on it... so the only way that the output would show in the cmd is if i execute the jar file through the cmd...
    so any suggestion guys on alternative solutions that i could implement?

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Try:

    Java Code:
    Runtime.getRuntime().exec("cmd.exe /c start cd ..");
    OR note the space before the c in cd and after the d to the ..

    Java Code:
    String[] commands = {" cd .."};
    String command="cmd.exe /c start"+commands[0];
    Runtime.getRuntime().exec(command);
    Last edited by al_Marshy_1981; 02-24-2011 at 05:07 AM.

  3. #3
    NaturalE is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Try:

    Java Code:
    Runtime.getRuntime().exec("cmd.exe /c start cd ..");
    damn... that was very stupid of me...

Similar Threads

  1. Replies: 7
    Last Post: 02-10-2011, 08:37 PM
  2. cannot find the class file when running the java command
    By kulangotski in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 12:34 PM
  3. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  4. Replies: 1
    Last Post: 06-30-2009, 03:32 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 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
  •