C# program wont be executed from Java - java

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 ?

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

Related

How to run a C++ exe file from Java?

I want to open an EXE file from a Java program. I tried 2 procedures.
The program can run some programs, like NotePad++, but cannot run my C++ EXE file. I tried:
Process exec = Runtime.getRuntime().exec(file.getAbsolutePath());
ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
but neither of the above work. No exception is thrown, and exec.isAlive = true.
Your mistake is that you took the absolute path in the first procedure.
Try using a relative path, I just tested and it worked just fine.
This also works for me (using 2 backslashs').
Runtime.getRuntime().exec("C:\\Program Files\\DDNet\\DDNet.exe");
Kind regards

How to Make Java Open a Batch File in Same Command Prompt Window

I am trying to make a console-based Java application that starts some batch scripts that do some other irrelevant things. Presently, I just want to find the proof of concept
I have tried to use the following code:
Runtime.getRuntime().exec("cmd /c start pathtomybatch.bat");
This works fine until I turn it into a .jar file and attempt to execute it. Then it opens the batch file in a new command prompt window, which I don't want it to do. I want to open the batch file in the same window that my Java program is running in. I read about the start command on TechNet and SS64 and found out that apparently adding changing start to start /b would open the program in the same command prompt window. However, when I try to run this:
Runtime.getRuntime().exec("cmd /c start /b pathtomybatch.bat");
NetBeans says BUILD SUCCESSFUL for both lines of code, but when I try the second line of code, no command prompt window opens and my batch file doesn't get started.
I want to know how I can make Java open that batch file within the same command prompt window without stopping the Java application or waiting for it to finish.
Also, as a tiny extra request, could someone tell me if I could do the same for an .exe file?
I'm on Windows 7, but I want this app to work for people using Vista or newer.
The extra window is coming from the start command you initiate. See https://www.windows-commandline.com/cmd-start-command/
A better pattern is to use
Process p = Runtime.getRuntime().exec("cmd /c pathtomybatch.bat");
Then make sure you loop until p.exitValue() no longer throws an exception (which means the process has exited), and while looping copy all available bytes from p.getOutputStream() and p.getErrorStream() to System.out and/or System.err.

Java visible command line

I just tried to execute a java command using runtime like this:
Runtime.getRuntime().exec("shutdown -s");
to shutdown the computer, but I am training on a project that will get both my electronics and computer skills so I want to execute an avrdude command to program the mcu from Java and make a GUI program. So I want the cmd window to be visible when I run the command. I just made it visible by:
Runtime.getRuntime().exec("cmd.exe /c start");
but I cant write on the window I just created, any help pls????
thanks all
Following would do it for you.
CMD.EXE /K your_command
I got an idea but still need some help!
What about making a .bat file and running it using java File class
but still when I make a bat file and try to run I get the command multiple times like this:
http://i.imgur.com/cMddMh5.png
but all I did was only write avrdude in the bat file:
http://i.imgur.com/PDbi2vJ.png
still need help in that!!
thanks for all answers...
EDIT::
I just looked it up in the internet and figured out a solution which is making a .cmd file instead of a .bat file and that worked great without looping thanks for all the repliers..!! :)

How can I run a bash script (which downloads a file) in Java?

Running in Mac OS X Lion, I need to retrieve a file from a remote server using a script in the command line. The command I'm trying to use in code is "bash /my/path/here/myscript" and I already run another process from the command line (atos) using the code below.
Process proc = Runtime.getRuntime().exec(cmd);
But while debugging, the program continues without error, yet the script does appear to have actually run. Furthermore, there should be a pause of several seconds while the script retrieves the file, yet my program continues to execute immediately. The script itself works as intended when run from the terminal. I'm a little stumped on how to get this to work, so any help would be greatly appreciated.
Got it to work with the following code -
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
proc.waitFor();
while (in.ready()) {
System.out.println(in.readLine());
}
The other thing that was an issue is that the script would download to the current working directory rather than the location of the script itself (as intended). So the script would run correctly while my program would continue to fail to find the downloaded file. Hope this helps.

Powershell process hanging when called from Java application

I am trying to write a simple application that takes in a command line arguement (which will be a Powershell ps1 file) and then run it. So I have experemented with a number of different approaches and seem to be running into a problem. If I attempt to invoke powershell from within java, the windows process is started and is visible via process explorer, however powershell never returns, it hangs in some sort of loop by the looks of it. The command I am using is:
String command = "powershell -noprofile -noninteractive \"&C:\\new\\tst.ps1\"";
The command is then executed using:
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
At the moment I am hard coding the location to the ps1 file as I was trying to rule this out as an issue. Using a process explorer I can see the hanging powershell process and the command that was passed to it was :
powershell -noprofile -noninteractive "&C:\new\tst.ps1"
which when copied into a cmd window, works to launch the tst.ps1 file. The file itself is incredibly simple in this example and I think I can rule it out being the cause of the freeze as I have tried to launch other ps1 files the same behaviour can be seen.
To further add to the confusion, if I use the java code posted above and pass in powershell commands instead of a file name then it successfully runs.
I've scoured the web and see lots of people experiencing the same issue but no one seems to have posted there solution, I hope its a simple oversight on my part and can be easily fixed.
Any hints/tips are appreciated :D
Alan
You have to close OutputStream in order for Powershell to exit.
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
proc.getOutputStream().close();
Is your external program writing to the standard outputs (err and out)?
If yes, it can hang waiting for you to consume them from the java parent process.
You can get those as InputStreams by calling
Process.getInputStream()
and
Process.getErrorStream()
There's more details here:
Javadoc for Process

Categories