For educational purpouses I am asked to use Java to call the execution of a .bat trough cmd.
As a starting point I did this little code, that for what I know should work, but executing the class does nothing, while running the .bat works as expected.
Java:
Runtime.getRuntime().exec("cmd /c start myDir.bat");
Content of myDir.bat (which is located in the same folder of the Java class):
dir > file_list.txt
Double click on the .bat generates and fills the file.
you can just execute : Runtime.getRuntime().exec("myDir.bat");
or provide absolutePath to your bat file.
You can also get OUTPUT and OUPUT ERROR from myDir.bat with java Thread.
If you are more than one parameter, you put an array string on exec method, example:
String[] p = {"cmd","/c","start","myDir.bat"};
Runtime.getRuntime().exec(p);
Related
I am running a java program using a .bat file. It works well after double clicking direcctly on the .bat, that's not the problem.
What I want now, is to run that .bt file (and the java program by extends) from the cmd at first, and then to be able to compile it from any other machine.
I have followed at first the following answer : How do I run a java program from a different directory? , but it didn't work for me.
Here is my .bat file :
#echo on
set CLASSPATH=%CLASSPATH%;.;lib/console.jar;lib/log4j-1.2.13.jar;lib/prog1.jar;lib/prog1_newOption.jar;lib/org.hamcrest.core_1.3.0.v201303031735.jar;lib/RXTXcomm.jar;lib/trace.jar;lib/xercesImpl.jar;lib/xml-apis.jar
java -cp "$/prog1_newOption/src/main/Main" %UsersCommand%
pause
exit
%cmd%
Maybe have I to look for the main path in the .bat file and then parse it the the "java command programm" line?
Thank you so much for your help
I have written a Java program that takes in arguments and then executes. I pass in these arguments from the command line (I am on a Macbook Pro using Terminal, using the bash shell). Let's say the name of my program is prgm. I want to be able to say "prgm " from any directory in the shell and then have that program execute. So I figure I need to write a bash script to reference the Java files and take in arguments, and put that bash script somewhere in my PATH. Where do I put the bash file, and where do I put my Java files? Also, am I right to assume that I only need the .class (binary) Java files?
Step-by-step:
Assuming that the name of the Java executable if myjavaprog.
Assuming that the name of your bash script is myscript.
Make sure myscript is calling myjavaprog using absolute path and the desired arguments.
call echo $PATH and you will see a bunch of paths: /some/path1:/some/other/path2:...
Put your bash script in whatever path you want from the ones returned by echo $PATH.
Go to a random path.
Call you bash script bash myscript. See the execution of myjavaprog.
Tips:
If java program is for personal use only, put it in a path starting with /usr/ or even in your $HOME directory (and add that location to your PATH)
If java program must be shared with other users, put it in an accessible place, so that other users don't need to modify their PATH variable.
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
Runtime.getRuntime().exec(new String[] {
"cmd",
"/c",
"start",
"cd",
"M:\\MandNDrives\\mwallace\\PROPHET\\PROPHET\\Prophet2012"
"prpht0912" //shortcut to prpht0912.exe
"eorinput" // eorinput.ind, input sheet that prpht0912.exe processes
Opens a command prompt to the dir I need.
To execute the program contained in that folder, I then need to execute "prpht0912 eorinput" from the command prompt like:
M:\MandNDrives\mwallace\PROPHET\PROPHET\Prophet2012>prpht0912 eorinput
However the space in the entry returns an error in the prompt: "The system cannot find the path specified"
It isn't possible to execute two commands via the command line in a single invocation of cmd.exe: cmd.exe /c is followed by a single command, and another /c afterwards would be interpreted as a parameter to that command.
Furthermore, invoking it twice won't get you what you want either, as changes of directory are forgotten when the process exits, so the second invocation would be run in the default working directory of the Java process, not the directory you changed to with your first invocation.
Also, it is unfortunate, but Java doesn't provide a way to change the current working directory of its own process.
As far as I can see it, you have two options:
Make sure your Java program is started with the working directory set to the one you need your child program to run in
Invoke a single .bat file that contains both of the commands you need to run.
To Execute the following command
M:\MandNDrives\mwallace\PROPHET\PROPHET\Prophet2012>prpht0912 eorinput
You need the following
String[] commands = new String[] { "cmd", "/c", "M:\\MandNDrives\\mwallace\\PROPHET\\PROPHET\\Prophet2012\\prpht0912.exe eorinput" };
Runtime.getRuntime().exec(commands);
Note**
When you pass an array, ProcessBuilder would consider only the first element as program and remaining as arguments for that program.
String prog = cmdarray[0];
I want to make a program that can execute jar files and print whatever the jar file is doing in my python program but without using the windows command line, I have searched all over the web but nothing is coming up with how to do this.
My program is a Minecraft server wrapper and I want it to run the server.jar file and instead of running it within the windows command prompt I want it to run inside the Python shell.
Any ideas?
First you have to execute the program. A handy function for doing so:
def run_command(command):
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b'')
It will return an iterable with all the lines of output.
And you can access the lines and print using
for output_line in run_command('java -jar jarfile.jar'):
print(output_line)
add also import subprocess, as run_command uses subprocess.