C# program wont be executed from Java

2019-08-08 21:38发布

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 ?

1条回答
孤傲高冷的网名
2楼-- · 2019-08-08 22:01

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
查看更多
登录 后发表回答