I just downloaded MCP to see how things work behind the scenes in Minecraft.
Inside MCP there are a bunch of batch files that you use to do things like: decompile, recompile, startclient, etc.
What I would like to be able to do is run these batch files from a basic java gui.
I'm good with the gui part but I havent got a clue how to run those batch files.
Here is an example of one of the batch files:
The file is at:
C:\MCP\startclient.bat
startclient contains the following:
#echo off
:try_python
set PYTHON=python
%PYTHON% --version >NUL 2>NUL
if errorlevel 1 goto try_python_mcp
goto foundit
:try_python_mcp
set PYTHON=runtime\bin\python\python_mcp
%PYTHON% --version >NUL 2>NUL
if errorlevel 1 (
echo Unable to locate python.
pause
exit /b
)
:foundit
%PYTHON% runtime\startclient.py conf\mcp.cfg
pause
Can it be done?
You can easily run a batch file from java using Runtime:
Process p = Runtime.getRuntime().exec("cmd /c start " + yourbatchFileName);
you can also grab I/O of the process, using p.getOutputStream(), p.getInputStream() etc.
See more about the Process class here.
And I suggest you take a look at this article as well.
Related
I want to check using a bat file if a Java program is already running. If its not running then start it using start javaw.
I have tried WMIC and I am only successful so far to get the PID.
WMIC process where "CommandLine like '%MyServer.jar%' and Name like '%javaw%'" get ProcessId
But how to write an if else condition in the bat file to start?
I tried using tasklist and find
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
But for all the java applications myapp.exe will be java.exe.
Please help...
P.S: There are options for this to be done via the Java program itself, but my specific situation asks for a bat file solution.
You can use jcmd.exe to see the main class of all the java processes running. The IF-ELSE syntax of the batch file is explained in the following thread
How to use if - else structure in a batch file?
jcmd | find /I /N "sun.tools.jconsole.JConsole"
IF "%ERRORLEVEL%" GTR "0" (
jconsole.exe
)
IF %ERRORLEVEL% EQU 0 (
echo Programm is running
)
This link would help with the IF construct - http://ss64.com/nt/if.html
I have a number of weka datasets I need to run at once so I was creating a batch file to do that.
#ECHO OFF
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
SET CLASSPATH=C:\Program Files (x86)\Weka-3-6\weka.jar
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
When I run the SET CLASSPATH command and the java command in a regular command line they work fine, and they also work on their own in a batch file but as soon as I nested them in a for loop, I started getting "\Weka-2-6\weka.jar was unexpected at this time" errors.
I'm not an expert in Java or in Batch file programming so forgive me if the fix is really simple but I've been up and down the internet and I haven't found any solutions to this problem. What am I doing wrong here?
Thanks for your help.
#ECHO OFF
setlocal
SET "CLASSPATH="C:\Program Files (x86)\Weka-3-6\weka.jar""
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
endlcoal
set class path can be expanded after the for loop is finished.So better defined it outside the loop.
I'm trying to run a batch file that included multiple jar files, Batch file includes 3 Jar files which executes one after another in one window, My batch file is working correctly for one record which fetches data from excel sheet.
Consider a scenario in which i have 5 records and i wanted to run the batch file in the manner like for 1st record->1st jar prog executes then 1st record->2nd jar file and finally 1st record->3rd jar file executes. Then this loop continues for the second record and likewise. Could anyone please help me to modify the below script which runs in loop and i want to save a executed results in a separate text file.
My script is below:
REM Run first and finish ...
java -jar first.jar
REM .. then start number two.
java -jar second.jar
REM .. then start number three.
java -jar third.jar
Kindly help!
You cloud do something like this that waits until the execution of one jarfile is done.
#echo off
java -jar 1.jar
pause
java -jar 2.jar
pause
If you want to run the jars sequentially, you can write a .bat file containing the following;
#echo off
java -jar first.jar
java -jar second.jar
java -jar third.jar
If you want to tun the jars simultaneously, you can write the .bat file as follows;
#echo off
start java -jar first.jar
start java -jar second.jar
start java -jar third.jar
START command will start running the jar in a new window.
You'll need something like
FOR %%A in (1 2 3 4 5) DO (
java -jar first.jar
java -jar second.jar
java -jar third.jar
)
That should execute the three jar consecutively five times. I didn't actually test this, but it should give you the idea. Here is an article on FOR loops syntax in batch files: http://www.robvanderwoude.com/for.php
I'm currently making an effort to create test cases for one of our java applications.
In my code, my java application calls a batch file, which in turn starts a separate java process, that returns an error code that I need to consume from the calling java application.
I'm doing the following to invoke my batch file:
Process process = runTime.exec(new String[]{"cmd.exe","/c",scriptPath});
exitValue = process.waitFor();
The batch file is as follows:
#echo off
cd %~dp0
java -cp frames.FrameDriver
SET exitcode=%errorlevel%
exit /B %exitcode%
Now with the above code and batch file, my JUnit framework just hangs on this particular test case, as if it's waiting for it to end. Now when JUnit is hanging on the test case, going to the Task Manager, and ending the java.exe process would allow the JUnit framework to continue with the other cases.
Running the .bat file by double clicking it runs the Java application normally.
Adding the START batch command before the java command in my batch file seems to fix the hanging problem, but I can't seem to get the correct exit code from my Java application as it's always 0. (The Java application exits with an error code using System.exit(INTEGER_VALUE)). I'm assuming that the %errorlevel% value is being overwritten by the "start" command's own exit value.
Can anyone please tell me how to solve this problem?
Thanks.
P.S: If it makes any difference, I'm using JDK 5 and Netbeans 5.5.1.
Don't use the /B on your exit. Here is how I would do a script:
#ECHO off
ECHO Running %~nx0 in %~dp0
CALL :myfunction World
java.exe -cp frames.FrameDriver
IF NOT ERRORLEVEL 0 (
SET exitcode=1
) ELSE (
SET exitcode=0
)
GOTO :END
:myfunction
ECHO Hello %~1
EXIT /B 0
:END
EXIT %exitcode%
NOTE: Also, you can execute java program in 3 different ways:
java.exe -cp frames.FrameDriver
CALL java.exe -cp frames.FrameDriver
cmd.exe /c java.exe -cp frames.FrameDriver
This is very critical since, your Java command may exit with a exit code and in order to pass the exit code correctly to the ERRORLEVEL var, you need to use the correct method above, which I am unsure about.
I have developed a Java socket server connection which is working fine.
When started from a terminal, it starts from listening from client. But when I close the terminal it stops listening.
I need to continue even though the terminal closed by user from where jar file was started.
How can I run Java server socket application in Linux as background process?
There are several ways you can achieve such a thing:
nohup java -server myApplication.jar > /log.txt - this is pretty straight forward. It will just put the application in the background. This will work but it's just not a very good way to do so.
Use a shell wrapper and the above OR daemon app. This approach is used by many open source projects and it's quite good for most of the scenarios. Additionally it can be included in init.d and required run level with regular start, stop and status commands. I can provide an example if needed.
Build your own daemon server using either Java Service Wrapper or Apache Jakarta Commons Daemon. Again - both are extremely popular, well tested and reliable. And available for both Linux and Windows! The one from Apache Commons is used by Tomcat server! Additionally there is Akuma.
Personally I would go with solution 2 or 3 if you need to use this server in the future and/or distribute it to clients, end users, etc. nohup is good if you need to run something and have no time to develop more complex solution for the problem.
Ad 2:
The best scripts, used by many projects, can be found here.
For Debian/Ubuntu one can use a very simple script based on start-stop-daemon. If in doubt there is /etc/init.d/skeleton one can modify.
#!/bin/sh
DESC="Description"
NAME=YOUR_NAME
PIDFILE=/var/run/$NAME.pid
RUN_AS=USER_TO_RUN
COMMAND=/usr/bin/java -- -jar YOUR_JAR
d_start() {
start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --exec $COMMAND
}
d_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case $1 in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
exit 0
There's one crucial thing you need to do after adding a & at the end of the command. The process is still linked to the terminal. You need to run disown after running the java command.
java -jar yourApp.jar > log.txt &
disown
Now, you can close the terminal.
The key phrase you need here is "daemonizing a process". Ever wondered why system server processes often end in 'd' on Linux / Unix? The 'd' stands for "daemon", for historical reasons.
So, the process of detaching and becoming a true server process is called "daemonization".
It's completely general, and not limited to just Java processes.
There are several tasks that you need to do in order to become a truly independent daemon process. They're listed on the Wikipedia page.
The two main things you need to worry about are:
Detach from parent process
Detach from the tty that created the process
If you google the phrase "daemonizing a process", you'll find a bunch of ways to accomplish this, and some more detail as to why it's necessary.
Most people would just use a little shell script to start up the java process, and then finish the java command with an '&' to start up in background mode. Then, when the startup script process exits, the java process is still running and will be detached from the now-dead script process.
try,
java -jar yourApp.jar &
& will start new process thread,I have not tested this, but if still it not works then twite it in script file and start i with &
Did you try putting & at the end of the command line?
For example:
java -jar mySocketApp.jar &
You can also use bg and fg commands to send a process to background and foreground. You can pause the running process by CTRL+Z.
Check it out this article: http://lowfatlinux.com/linux-processes.html
Step 1.
To create new screen
screen -RD screenname
Step 2.
To enter into screen terminal
press Enter
Step 3.
Run your command or script (to run in the background) in the newly opened terminal
Step 4.
To come out of screen terminal
ctrl + A + D
Step 5.
To list screen terminals
screen -ls
that will print something like below
There is a screen on:
994.screenname (12/10/2018 09:24:31 AM) (Detached)
1 Socket in /run/screen/S-contact.
Step 6.
To login to the background process
screen -rd 994.screenname
for quite terminal and this process still working background. for me, the simple and fast way to run the process in the background is using the &! at end of the command:
if this app is built for X server: (eg: Firefox,Zathura,Gimp...)
$ java -jar yourApp.jar &!
if this app is cli (work on the terminal)
# st is my terminal like kitty alacritty
$ st -e bash -c "lookatme --style one-dark --one $1" &!