4
I’m having trouble running some commands through java. Whether I run them directly from Netbeans or run the project directly from my machine works perfectly any of the following commands:
String comando = "cmd /c \"//Server/Sistema/Scripts/executa.bat\"";
exec = Runtime.getRuntime().exec(comando);
or
String comando = "cmd /c \"tskill servico\"";
exec = Runtime.getRuntime().exec(comando);
But as you can see . bat as well as the system, will be on a server on the network and only the shortcut is available to the user. So when testing on a user machine or the command does not work, or I have to run it several times for it to work, remembering that if I go in the script folder and run it manually the same will work.
The strange thing is that on the same button that I call the method of this operation, I also call another method where I open a page on Chrome with the command:
String comando = "cmd /c start chrome.exe http://link";
and this works perfectly, no matter how many times I click.
I don’t know if this could be some network problem, or the script/command when executed by java, run on the server and not on the user’s machine where only the shortcut exists. All users have permissions for the system folder.
I also tried to use the command as follows:
String[] comando = {"\"//Server/Sistema/Scripts/executa.bat\""};
ProcessBuilder builder = new ProcessBuilder("cmd", "/c",
String.join("& ", comando));
Process p = builder.start();
But the same thing happens, direct from Netbeans it runs, but by jar, I have to run several times.
If anyone has any suggestions thank you.
I will try in the manner quoted in that question, thank you!
– Andre Hoffmann
From what I understood in the other answer he does basically the same thing as me, the only difference is that he wants to show the return. I even have it here. My problem is also not to run multiple commands at once, the problem is not to work, or to have to call the same command several times until it works. What I can do is maybe try to add a loop that if it didn’t work run the command again, until it works..
– Andre Hoffmann
To be more exact I also executed the command as follows:
ProcessBuilder builder = new ProcessBuilder("cmd", "/c",
 String.join("& ", comando));
as in the other question and did not solve.– Andre Hoffmann
What code is returning on execution? What is the return of: exec = Runtime.getRuntime(). exec(command); exec.waitfor(); int returnValue = exec.exitValue(); System.out.println(returnValue);
– Diego Schmidt
Returns nothing because the Netbeans command:
String comando = "cmd /c \"//Server/Sistema/Scripts/executa.bat\""; exec = Runtime.getRuntime().exec(comando);exec.waitFor(); int returnValue = exec.exitValue(); System.out.println(returnValue);
works normally.– Andre Hoffmann
You said you created a shortcut, but you’re running the bat. Create a shortcut and place it on the desktop for example and run as follows: String command = "cmd /c "C: Users Diego Desktop test.lnk""; And run your application as administrator. That way I managed quietly.
– Diego Schmidt
I have both the system and the scripts on the server. I created a system shortcut, the .jar. But I will test it.
– Andre Hoffmann
I think I managed to solve the problem, which apparently was in the script. I changed it to
tasklist | find /i "servico.exe"
IF %ERRORLEVEL% == 0 (taskkill /f /im "servico.exe"
taskkill /f /im "servico2.exe"
ping -n 2 localhost>nul
)
and it worked, both in Netbeans and outside, only with a shortcut on the client machine. Thanks for the help!– Andre Hoffmann