I am trying through Java to call a batch file in another folder directory.
String cmd = "cmd /c start /wait " + backupFolder + "\\script_encrypt.bat";
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
However, when the batch file runs, it shows me the current directory which the batch is not there, why?.
Are you sure the batch file path and the path called from Java are the same?
According the screenshot from Eclipse and the screenshot from Cmd, they are not matching.
I have done the same, and worked for me.
Related
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);
I'm not too familiar with programming for the Windows desktop and I'm trying to figure out how to handle calling .bat files from Java. I'm trying to make desktop Java program using Maven run as a jar file execute a bash script in a Citrix virtualization Environment.
The original initial problems was that UNC paths were causing to the batch file to not be found, but I've modified the paths and now I'm able to find the batch file, but cmd.exe is disabled in the Citrix account I'm using, and I worried that there's nothing I can do.
Here's how it works:
1)
User launches program via program.jar file User checks for updates
2)
If updates are found, a new jar file called program_update.jar is
downloaded, and a batch file called run_update.bat is copied to the
main program directory.
3)
From within the java program, the following command is called:
Runtime.getRuntime().exec("cmd /c pushd " + FileName.installation + " && start run_update.bat && popd");
where FileName.installation is a property that has been previous set indicating the home installation directory.
run_update.bat runs a loop attempting to replace program.jar with program_update.jar and show a success/fail message in the command prompt.
When I run this in the Citrix environment, I get the following message:
'run_update.bat' is not recognized as an internal or external command, operable program, or batch file.
The command prompt has been disabled by your administrator.
At this point, I can click on run_update.bat and it runs the operation successfully.
The automatic update called via program.jar works in a normal windows environment with access to cmd.
My questions
Is my call from java to run cmd /c <commands> simply not going to work when cmd.exe is disabled?
Would renaming run_update.bat to run_update.cmd do any good?
Any alternative techniques? The batch file is necessary AFAIK because the jar file is replacing itself via the mv command.
I solved my own problem with an alternative technique. It turns out running Runtime.getRuntime().exec("cmd /c pushd " + FileName.installation + " && start run_update.bat && popd"); in a Citrix Environment was the problem, becase access to cmd is restricted. I rewrote it to use the the Desktop class:
File exe = new File(FileName.installation + File.separator + "install_update.bat");
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(exe);
} else {
Runtime.getRuntime().exec("cmd /c pushd " + FileName.installation + " && start install_update.bat && popd");
}
I want to create auto-updater of my program.
In java part it looks like
int pid = Kernel32.INSTANCE.GetCurrentProcessId();
String cmd = folder + "update.bat" + " " + currentLoc + " " + updateLoc + " " + Integer.toString(pid);
Runtime.getRuntime().exec(cmd);
And the batch contains
SET "name=GameDrive Logs Viewer.exe"
SET "myname=update.bat"
TASKKILL /pid %3
TASKKILL /pid %3
DEL "%1\%name%"
MOVE "%2\%name%" "%1"
"%1\%name%"
DEL "%2\%myname%"
So, I'm killing current program and delete it.
Then i move new version to old folder, run new version, and delete the bat file.
This bat file is perfectly works when i call it from cmd with sending parameters.
But nothing is happend when i'm trying use it from java program.
As i found, that all Dialog windows creating from current program have the same processID. (I tested it from another bat).
So, my guess is the batch which is called from my java program get the same processID and kill himself.
Am I right? And if yes - can how I do that?
I guess you need launch your update.bat in another cmd instance as follows (add path as necessary). In JAVA use updatecall.bat with next content:
Either with CMD.exe: Start a new CMD shell and (optionally) run a command/executable program.
cmd /C ""update.bat" %*"
or with START: Start a program, command or batch script (opens in a new window.)
start "" "update.bat" %*
If started a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will tend to remain after the batch has been run. To auto close it, add EXIT command to the end of started batch.
I'm sure there is a smarter solution without any auxiliary batch...
I'm trying to run this command:
"C:\arduino\arduino --upload --board arduino:avr:nano --port COM3 -v ..\config\config.ino"
It works from my command line. It takes just a few seconds to run & finish.
The problem comes when I try to execute it from Java with:
String cmd = "C:\arduino\arduino --upload --board arduino:avr:nano --port COM3 -v ..\config\config.ino"
Process p = Runtime.getRuntime().exec(cmd);
It takes a minute to execute and ends with:
Launch 4j: an error occurred while starting the application
I've tried with ProcessBuilder too. I've also tried saving the command to a batch file and then running the batch file from Java...but I just got the same result.
EDIT:
If I run the batch file from command line it works too. As I said if I run it from Java, it doesn't.
I've realized that if I run the batch file from another batch file it doesn't work neither.
Maybe there is not a problem at all with Java, but with Arduino IDE.
EDIT 2:
Adding "start" parameter before the command and saving it to a batch file seems to work. Then you just run the batch from java.
Something like this:
arduino.bat
"start C:\arduino\arduino --upload --board arduino:avr:nano --port COM3 -v ..\config\config.ino"
Java code
String s = "PATH TO ARDUINO.BAT"
Process p = null;
ProcessBuilder pb = new ProcessBuilder(s + "arduino.bat");
pb.directory(new File(s));
p = pb.start();
I think the issue is with the relative path at -v argument. Use full path or set
actual working directory with ProcessBuilder.
How to change content of batch file using Java code?
I worked with parsing XML using Java program. It worked fine. But can I do same for the batch file using Java?
I am able to run batch file using below code.
String command = "cmd /c start " + batFile;
Runtime rt = Runtime.getRuntime();
rt.exec(command);
The content of my batch file is:
#echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C "C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml"
exit
But before doing this I want to change location
C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml
..to user given XML location. How can I achieve this in my Java application?
Maybe use an environment variable instead. This way you don't need to edit the batch file, just set the variable before running it.
Here or here see how to set an environment variable from java and here's how to use them in a batch file.
How about passing command line args to the bat file like so:
#echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C %1
exit
Then modify your java code to pass in the XML file name after the bat file name