1

I have this C# program i made and while i can run it fine by clicking the exe file or by clicking on a batch file, I cant start up the program on a java program I made to run it. I have tried this line of code and couldn't get the software to run.

Runtime.getRuntime().exec("nameOfTheExeFile");

or set it to the batch file i made that starts the program.

Runtime.getRuntime().exec("nameOfTheBatchFile");

Now the interesting thing is when I try it with the batch file i get an error saying that the file cannot be found but when i double click the batch file it will start the exe file just fine.

I have even tried to use Process but I am not getting any luck with that process as well

List cmdAndArgs = Arrays.asList(new String[]{"cmd.exe", "/c", "ProgramName.exe"});

ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);

Process p = pb.start();

Strange thing is i dont get any error at all. Even when i try unit testing i don't any error's at all. Is there a process I am missing or something ? I am lost on what to do.

Update:

When i check on the task manager i can see that the program is running but not the exe version. I see ProgramName.vshost.exe , is there a reason for this to be showing and not the exe file ?

6
  • 4
    Can ProgramName.exe be found on your PATH? Commented Jan 15, 2015 at 18:29
  • Yes it can and it is, i dont get a errror saying that the file is not there or that i can't find the batch file. And the batch file is in the same directory as the exe file. Commented Jan 15, 2015 at 18:40
  • I have the directory set right i made sure of that as i can set the directory and the list and still find the batch file Commented Jan 15, 2015 at 18:42
  • Do you have access to the repertory where the file is located ? Som times, things like this can block your way Commented Jan 15, 2015 at 19:00
  • I thought i do, but then again i am not sure, The Junit testing when i try to use the batch file i get the windows error saying the exe file can't be located. But i can run it fine just by clicking on the batch file. I do not know why that is happening. Commented Jan 15, 2015 at 19:11

1 Answer 1

1

Since your program is command line program you need to start it from cmd. I'm not sure if this is the best way to do it, but it works.

Runtime.getRuntime().exec("cmd /c start nameOfTheBatchFile");

Batch file:

start cmd.exe /k "nameOfExeFile"
exit
Sign up to request clarification or add additional context in comments.

8 Comments

If the path is wrong wouldn't I get a null error? I am not getting that error. other then when i try to run it from the batch file.
Runtime.getRuntime().exec("nameOfTheExeFile"); will give you IOException but that processbuilder won't throw any exceptions even if the file is not found. You can try it yourself.
Well that is odd when i try that I am getting a file doesn't exist error but why would java give me that, and before you ask yes the exe file is there :P , I tried that with the batch file and while i can still run the batch file the exe file doesn't want to run.
Can you post the output of the code i posted above?
the output is just a windows popup prompt saying windows cannot find the program. Thats when i use your example and try it with the batch file.
|

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.