Program started from batch file not showing GUI when executed within Java - java

I'm new of Stack Overflow but used it so much to learn lots of things. I looked a lot for a solution of my problem and with no luck.
Anyway, the situation is:
I have a servlet that must execute a batch file to execute some operations such as start a program.
Here is the code o the servlet:
StringBuffer batchCommand = new StringBuffer(TerminalProperties.getRunningStorePath());
batchCommand.append("file.bat");
Runtime runtime = Runtime.getRuntime();
batProcess = runtime.exec(batchCommand.toString());
The content of the batch file is:
taskkill /f /im program.exe
cd C:\folder
start /MAX /WAIT /HIGH program.exe
Everything goes well when I run the batch file from a command line or with double-click. If the program.exe is running, it closes and then is restarted. When it executed from java servlet everything works but the program is started IN BACKGROUND; I need it to show the GUI.
I found some problems similar but related to task scheduler, that is not my problem.
I tried use runas /user:Administrator program.exe in the batch file but with no success.
Tried also to start the program with java code:
runtime.exec("cmd /c \"cd C:\folder & start /MAX /WAIT /HIGH program.exe\"");
but nothing happens...
Any ideas how can I fix it? Thanks for your time.

Related

Keep info of new javaw process to close it later using cmd

I made a program in java that changes your windows wallpaper at a certain time of the day, this runs the UI and a thread in the background that checks if the wp should be changed or not.
I'm trying to figure out a way to keep track of that background thread and close it using a .bat file.
I run the program like this:
start javaw -jar wchanger.jar
And this is the solution I came up with:
taskkill /f /im "javaw.exe"
The problem with this is that closes every javaw process running on the pc.
I though I could name the process whatever I want, but that doesn't seem to be possible on windows.

Java launch independent process

I have a java app from which I run console based programs on linux system, I am reading the output of those programs and then my java app is sending it to a webpage.
But once I close my java app all the processes will get "stuck" or they just simply crash. So everytime I want to make some changes to my java app and I need to restart it I also have to close all processes that were running from my app. I would like to save their PIDs when closing my app and then take control (output streams) over those processes again based on saved PIDs of the processes.
Is there any way to do it?
I am running my programs like this:
ProcessBuilder processBuilder = new ProcessBuilder(new String[] { "su", "-
s", "/bin/sh", "myuser", "-c", "java -jar myjar.jar" });,
Process p = processBuilder.start();
Edit:
My problem is not finding the process PID my problem is that my subprocesses lanched from my java app are crashing after my java app is closed/terminated and I need them to continue running even while my app is restarting/stopped.
Your problem is due to what is called Unix job control.
Like many shells do, /bin/sh intercepts SIGHUP and SIGINT signals, and before exiting, it sends signals to some of its child processes groups, depending on its configuration and on their state (for instance, stopped background processes receive a SIGCONT).
So, when your main java app is closed, the /bin/sh shell that your app had forked is terminated, and just before exiting, it sends a SIGHUP signal to its subprocesses corresponding to the command java -jar myjar.jar.
So, the answer to your question is: just use the huponexit /bin/sh shell option to avoid killing subprocesses. They will be detached from the controlling terminal, if any, but they will not be killed.
So, replace this java -jar myjar.jar by shopt -u huponexit; java -jar myjar.jar:
ProcessBuilder processBuilder =
new ProcessBuilder(new String[] {
"su", "-s", "/bin/sh", "myuser", "-c",
"shopt -u huponexit; java -jar myjar.jar"
});
Process p = processBuilder.start();
Try to run your command like this:
Runtime.getRuntime().exec("gedit");
It executes the specified string command in a separate process.
Some time ago I have found some useful information here. Try this.
I would use the ps command in Linux to get the number of each process(s) running which you want to control, of course you would execute it just as you have above with your ProcessBuilder. I would then pipe; "|" (Linux Command), the output into a file you have saved somewhere in your Java project.
The Linux command to execute from your Java program would look something a long the lines of
ps -A | grep "your_program_name" > /path/to/your/project/my_process_list_file.txt
Where the > stores the output of the command executed to your file.
I would then read from this file and execute some other Linux commands to take control of that process in whichever way you desire.
Good Luck, and happy coding my friend!

Using Java code to run multiple commands with Runtime/Processbuilder

I'm trying to do something very simple here, but it isn't working.
Basically I have a program and an input file sitting at a certain directory, let's call it "programDir".
I'm writing a plugin in Eclipse that will call this program and run it on the input file.
Essentially two steps must be done: 1) cd to programDir 2) run the program by calling "idp input.txt"
I have done this manually in the cmd and it works as expected. However in Java I can't get it to work. I tried 2 approaches:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd.exe /c cd \""+programDir+"\" & \"idp input.txt\"");
Here I get the following from the errorStream: "idp output.txt" is not recognized as an intern or extern command, program or batch file
I'm not sure why. I have left out the second part of the command to ensure that I am in the correct location. When i add "start", a console window pops up and it is in the programDir folder. If I then manually type "idp input.txt" I get the expected behavior.
Second approach:
I also used ProcessBuilder after some googling on the subject. I tried this piece of simple code:
String[] command = {"CMD", "/C", "idp input.txt"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File(programDir));
Process proc = pb.start();
Now nothing happens and the program does not terminate.
Again, if I add "start" as one of the parameters in "command", a console window pops up in the right location and if I then manually type "idp input.txt", it works. So I have no idea why the code doesn't work.
One interesting thing: the idp.bat file calls a kbs.exe process. When I run the second piece of code, no kbs.exe appears in my task manager. However, as soon as I terminate the program, it briefly makes an appearance. Does that mean my code gets stuck in a loop somewhere or something?
Any help appreciated!

launching java program via batch and php,

php and batch files.
So i'm executing batch files through php
I have no issues launching the batch.
$str = exec('C:\WINDOWS\system32\cmd.exe /c START C:\MInecraft\_restart.bat');
However the issue im having is getting the batch file to work currectly. When the batch is run, it executes this code
taskkill /IM java.exe /F
java -Xmx1024M -jar craftbukkit.jar -o true
batch file successfully runs the taskkill command when launched with php, however it will not run the next line. When ran manually it launches fine, (bear in mind that the cmd.exe does not exit it stays open with this code)
Any ideas on how i can get this to launch from php?
Maybe the reason is the same as given at Error Executing Batch file of BlazeDS, the java executable is not found by Windows on running the batch file from within PHP script.
The current working directory or the directories listed in environment variable PATH can be different when the batch file is executed from within the PHP script in comparison to running the batch file manually.
Do you have ever tried to specify java.exe with full path in double quotes in the batch file?
You could also add at top of the batch file the commands
dir /w
PATH
to see which directory is the current directory and which directories are listed in environment variable PATH on execution of the batch file from within PHP script.
Forgot to post back here, I specified java through windows path command rather than the batch. The problem is because the java process takes the cmd and turns it into a console, it never finishes the batch file. So, the php never returns and continues the code unless the batch is force ably closed on the server.
pclose(popen("start /B C:\MInecraft\_restart.bat &", "r")); die();
I ended up using this command, which i believe makes it launch the batch and not wait for a reply. Just carry on with the php.
Thanks for your kind reply. I did initially try launching java in multiple ways.

Calling Scripts from Java through SFTP Connection

I am newbie for calling scripts from Java through the SFTP connection.
So far, I managed to find the code snippets below
Process p = Runtime.getRuntime().exec("cmd /c start Hello.bat");
p.waitFor();
Here is sample for Hello.bat
#echo off
echo "Hello World"
However, I couldn't see the output in cmd window eve there were no errors.(seem like Hello.bat file location is not right?)
My actual and final script include copying, reading, archiving, delete and return code for success or fail.
What type of script with the above methods will be fine and I hope some one will advise me for the right direction with working sample.
Thanks and best regards
just Runtime.getRuntime().exec("cmd /c start Hello.bat") works for me. Also see How do I run a batch file from my Java Application?
Make a small change in batch file to keep command prompt open
#echo off
echo "Hello World"
PAUSE
so your java code is fine you just need to modify batch file to keep command prompt open when program is run successfully.

Categories

Resources