0

I´am facing a very strange Problem right now. It took me 2 hours to reproduce and get the "solution" of this error.

This happens:

I try to start another .jar file, located in System.getProperty("user.dir") .

actually, I use this for start:

Runtime.getRuntime().exec("java -jar "+System.getProperty("user.dir")+System.getProperty("file.separator")+"myJar.jar");

This works fine if the System.getProperty("user.dir") 's will look like this for example:

C:\MyProgramm\

But if the System.getProperty("user.dir") look like:

C:\My Programm\

it will not work. It´s strange, beacuase there is no exception thrown. Please, if you can, and if my Explanation is clear enough, can you give me a solution for this issue?

Sorry for my english, please do not hesitate to ask all questions if something is unlear.

0

3 Answers 3

2

You shall put your commands in a String[] like this:

String[] command = new String[]{"java","-jar",System.getProperty("user.dir")+System.getProperty("file.separator")+"myJar.jar"};

and than use

Runtime.getRuntime().exec(command);

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

if my Explanation is clear enough, can you give me a solution for this issue?

When you give a single string for it to run it expects to use space as a seperator e.g.

java -jar C:\My Program\myJar.jar

has four words

java
-jar
C:\My
Program\myJar.jar

The simple solution is to not get it to do the parsing.

Runtime.getRuntime().exec(new String[] {
    "java", "-jar", System.getProperty("user.dir")+System.getProperty("file.separator")+"myJar.jar" } );

Now it no longer sees space as a separator.

When you run a program, errors are sent to the error stream, and do not trigger an exception. I suggest you always read the error stream to see what errors you might be getting.

4 Comments

Compiler said "no suitable method found for exec(String,String,String)
In that case try Runtime.getRuntime().exec(new String[] { "java", "-jar", System.getProperty("user.dir")+System.getProperty("file.separator")+"myJar.jar" } );
@ÂlexBay Which version of Java do you have?
I use JDK 1.7.0_55, I will give it a try! edit: Yes, this methods works also very good. Thank you very much! upvoted
1

You have to use quotes around a path if it contains spaces.

java -jar "C:\My path has spaces\my app.jar"

In your specific case:

Runtime.getRuntime().exec("java -jar \""+System.getProperty("user.dir")+System.getProperty("file.separator")+"myJar.jar\"");

Where \" is the escape character for ".

Read more here about escape characters

3 Comments

I do not hard-code the path, I did it this way: Runtime.getRuntime().exec("java -jar "+System.getProperty("user.dir")+System.getProperty("file.separator")+"myJar.jar");
@ÂlexBay Read the answer, I updated it in the exact moment you wrote your comment :)
:) seems to work like a charm, I will do some checks and mark this answer as right if I don´t face a Problem. Thank you in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.