Forum Stats

  • 3,874,546 Users
  • 2,266,752 Discussions
  • 7,911,885 Comments

Discussions

problem in executing jar when the folder name has empty space

843810
843810 Member Posts: 46,938
edited Jul 15, 2010 1:08PM in Java Desktop Applications
Hi,
I need to run my java swing application on double clicking the jar file. It works fine when the file path has no empty spaces in the folder name. It doesnt open when the folder name has spaces in between them. Can anyone let me know how to resolve it.
String workingDir = new File("SwingApplication").getAbsolutePath(); 
String cmd = "java -jar " ++workingDir ++"/SwingApplication.jar"; 
Runtime.getRuntime().exec(cmd);
This works fine when the SwingApplication.jar is inside for eg: c://{color:#ff0000}RunApplication{color}/SwingApplication.jar
but the application doesnt open if the file path is c://{color:#0000ff}Run Application{color}/SwingApplication.jar

Comments

  • 843810
    843810 Member Posts: 46,938
    edited Jul 15, 2010 8:02AM
    Try this
    String cmd = "java -jar \"" ++workingDir ++"\"/SwingApplication.jar"; 
    Edited by: DynamicBasics on Jul 15, 2010 5:31 PM
  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    A good case for using ProcessBuilder.
  • 843810
    843810 Member Posts: 46,938
    String cmd = "java -jar \"" ++workingDir ++"\"/SwingApplication.jar";

    doesnt seem to work, so i tried using process builder as suggested

    ProcessBuilder pb = new ProcessBuilder("java -jar");
    pb.directory(new File(workingDirPath + "/SwingApplication.jar"));
    Process p = pb.start();

    . but getting an exception

    java.io.IOException: Cannot run program "java -jar" (in directory "C:\Run Application\SwingApplication.jar"): CreateProcess error=267, The directory name is invalid

    Do I need to change the directory path in this?
  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    javaquests wrote:
    ..ProcessBuilder pb = new ProcessBuilder("java -jar");
    pb.directory(new File(workingDirPath + "/SwingApplication.jar"));
    Process p = pb.start();

    . but getting an exception

    java.io.IOException: Cannot run program "java -jar" (in directory "C:\Run Application\SwingApplication.jar"): CreateProcess error=267, The directory name is invalid
    While..
    C:\Run Application\
    ..might be a directory, is..
    C:\Run Application\SwingApplication.jar
    ?

    I was thinking something more like this (untested)..
    String path = workingDirPath + "/SwingApplication.jar";
    String[] command = {
    	"java", 
    	"-jar",
    	path
    };
    ProcessBuilder pb = new ProcessBuilder(command);
    Process p = pb.start();
    BTW - when posting code, code snippets, HTML/XML or input/output, please use the code tags. The code tags protect the indentation anf formatting of the sample. To use the code tags, select the sample and click the CODE button.
  • 843810
    843810 Member Posts: 46,938
    edited Jul 15, 2010 1:07PM
    javaquests wrote:
    String cmd = "java -jar \"" ++workingDir ++"\"/SwingApplication.jar";

    doesnt seem to work, so i tried using process builder as suggested
    The command to be finally executed is
    C:\Program Files\Java\jdk1.5.0\bin\java.exe -jar C:\Run Application\SwingApplication.jar
    as the path has spaces, enclose in double quotes
    "C:\Program Files\Java\jdk1.5.0\bin\java.exe" -jar "C:\Run Application\SwingApplication.jar"
    This goes into the string variable like this
    String cmd = "\"C:\\Program Files\\Java\\jdk1.5.0\\bin\\java.exe\" -jar \"C:\\Run Application\\SwingApplication.jar\"";
    check the above command once.

    Without the absolute path to the JAVA file, the control probably is searching for it in the current working directory.
  • 843810
    843810 Member Posts: 46,938
    thank you so much it worked now.
  • 843810
    843810 Member Posts: 46,938
    edited Jul 15, 2010 1:08PM
    javaquests wrote:
    thank you so much it worked now.
    You are welcome, and it is a good practice to mark the question as 'answered'.

    Edited by: DynamicBasics on Jul 15, 2010 10:38 PM
This discussion has been closed.