.BAT files: Launch the JVM in background - java

Having read the post at .bat files, nonblocking run/launch,
I still cannot achieve what I need: to have the .BAT file close once the start command is executed. My problem is that when the JVM starts the application launches a window so I end up with 2 windows being opened, when in fact one of them (the .BAT command) is just a startup process and doesn't do anything meaningful to the user.
I paste the .BAT code here:
#echo off
setlocal
rem Starts the application
rem Check for Java Home and use that if available
if not "[%JAVA_HOME%]"=="[]" goto start_app
echo. JAVA_HOME not set. Application will not run!
goto end
:start_app
echo. Using java in %JAVA_HOME%
start "Application" "%JAVA_HOME%/bin/java.exe" -jar lib/pathToMyJarFile
goto end
:end
I'd like the .BAT process to terminate (or at least the window to close) once the JVM starts.

Try javaw.exe instead of java.exe.

Use start /b, see this:
http://zeroflag.wordpress.com/2007/05/12/start-command/

Related

Need help running my Java .jar file from Windows batch file

I have a Java jar file located in:
C:\Users\myusername\bin\MyDir\MyApp.jar
I also have some required properties files (needed as input arguments to the .jar file) located in the same directory as the .jar file.
I created a runme.bat file here:
C:\Users\myusername\Desktop\runme.bat
In the runme.bat file, this is what I have:
setlocal
set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_161\bin\"
set PATH=C:\Users\myusername\bin\MyDir\
start %JAVA_HOME%javaw -jar %PATH%MyApp.jar %PATH%propertiesfile.properties
However, whenever I try to run the .bat file, I get the error:
Windows cannot find '-jar' Make sure you typed the name correctly, and then try again.
On the command line I see Windows trying to do this:
> "C:\Program Files\Java\jdk1.8.0_161\bin\"javaw -jar C:\Users\myusername\bin\MyDir\MyApp.jar ...
I get this error when running from the command line. If I simply double-click the .bat file, a cmd window comes up and quickly disappears.
So, what am I doing wrong?
Thanks!
Use Double quotes around the set command, not inside the variables.
Also, I see no reason to use the START command unless you want to do more in your batch file in the original command prompt after starting your Java in a second command prompt. Possible but seems unlikely.
Generally, you will just type in the executable or use CALL so that the executable runs and control returns to the batch after reaching conclusion.
Additionally, you changed your system path variable to be just the path of your java files which will make the session pretty screwy. Thankfully this should only persist in your open command windows and those spawned by the original window, so close them all and then use a different variable name for your path.
So I will put this both ways, using Call, and using start.
Here is your code using call:
#(
setlocal
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
TITLE "%_Title%"
CD /D "%JAVA_HOME%"
CALL "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%\propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)
Here is your code using the start command:
#(
SETLOCAL
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
start "%_Title%" /D "%JAVA_HOME%" "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)

Default directory when navigating to a bat (or any) file

I have a run-tests.bat file as in the following (provided to us). This bat file is in a directory along with a lot of other files and is zipped. It is a standalone testng directory. In addition to this file, in the directory are subdirectorys lib/ and drivers/ and lib/ does have testng:
#ECHO OFF#ECHO OFF
SET javacmd=
SET javacp=-classpath ".;lib/*"
SET jvmparams=-Dwebdriver.ie.driver="drivers/IEDriverServer.exe" - Dwebdriver.chrome.driver="drivers/chromedriver.exe"
IF "%JAVA_HOME%"=="" (
SET javacmd=java
) ELSE (
SET javacmd="%JAVA_HOME%\bin\java"
)
for /f tokens^=2-5^ delims^=.-_^" %%j in ('%javacmd% -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"
IF %jver% LSS 17000 (
ECHO Java version is not supported. Please install Java 1.7 or greater.
GOTO RETURN
)
%javacmd% %javacp% %jvmparams% org.testng.TestNG testng.xml
:RETURN
IF -%1-==-- (
PAUSE
)
When I run this on my machine it works fine. I needed to copy it to a shared directory so some other people could run it. I copied it to a shared directory and tests it and it ran fine. I navigated with the file explorer, and double clicked.
When I had the tester do it from his machine, the cmd window would just come up and disappear. I put a bunch of pauses in it to see where it was failing. The %jver% is 17045 so it made it past that line. It failed at the %javacmd% command and said it could not find (or load) org.testng.TESTNG. It found it fine when I ran it from my machine but not from his.
My only thought is that when I double click on the .bat file it somehow CD's me to the correct directory, but leaves him in his directory so that SET
javacp=-classpath ".;lib/"* sets lib/. in his own directory rather than in the directory that is unzipped. I tried to figure out when you click on a .bat file whether it CDs you to a directory or not but could not find it out.
So is my theory for why it is not working correct, or is there some other reason I am not seeing? I verified my dos version and his are the same, 6.1.7601, and we are both using Windows 7 and he has Java 7.
Double clicking a .bat or .cmd script file causes that script runs usually
either in %HOMEDRIVE%%HOMEPATH% or in %SystemRoot%\system32 starting directory. I'd try next
#ECHO OFF
SETLOCAL EnableExtensions
echo "%~f0" script started in "%CD%"
pushd "%~dp0"
echo "%~0" script now run in "%CD%"
pause
rem continue here with your code:
SET javacmd=
…
Explanation:
SETLOCAL EnableExtensions: although Command Extensions are enabled by default, this can be disabled or switched off, see CMD shell or cmd /?. Right ensure…
"%~f0", "%~dp0" Parameter Extensions: see Command Line Arguments or call /?.
pushd "%~dp0": change the current directory/folder including disk drive, see also pushd /?.

Get exit code from a java application in batch file

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.

Start a java program without the console

I am trying to use this GUI mod for a Minecraft Server. I wrote a batch file so the server can start with more RAM. When I run just the .jar file, no command window opens and it runs just fine (of course with about 256mb ram) I was reading online that javaw starts a jar file without a command-line-console. But when I use javaw, the command console opens, but when I close it the program remains open. this is my batch file:
#echo off
"%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe" -jar -Xms1024m -Xmx1024m crafty.jar
#echo on
I don't understand java as well as most, so please try to be as clear as possible. Thanks
If you want to start a java program without console popup under windows, this should be helpful:
In command prompt type the following:
start javaw -jar -Xms1024m -Xmx1024m crafty.jar
If you want you can also write this as a batch file.
You should Create Shortcut of "%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe", let's name it as Minecraft, then
edit the Properties of Minecraft shortcut. In the Target textbox, append -jar -Xms1024m -Xmx1024m crafty.jar in the end of javaw.exe
change the Start in as the folder which contains the crafty.jar
Double-click the Minecraft icon to star the server.
That's all.
Create a .bat file with
start javaw -jar yourjar.jar arg0 arg1
start javaw -jar yourjar.jar arg0 arg1
will open the console, but close immediately. it is different from running window .exe.
You will always get the command window opening and closing because you are starting it inside a command window or batch script (which launches an implicit command window to run itself).
In order not to get a command window you must open the file from "not a command window" i.e. an executable launcher.
Take a look at Launch4j which can run a java program from an exe. It can also hide-away the jar file inside the exe if you like.
http://launch4j.sourceforge.net/
There's a little YouTube clip showing them creating an exe from a jar.
A batch file is a way of starting the command prompt with the code pre-written, using javaw is a way of open then closing the prompt. Like I said a batch is a commands prompt you can't stop it from opening.
It's few years, but for windows today as for linux you have the supervisor (pytho based)
supervisor windows based on python

how to start stop tomcat server using CMD?

I set the path for the tomcat and set all variables like
JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_22
CATALINA_HOME=G:\springwork\server\apache-tomcat-6.0.29
CLASSPATH=G:\springwork\server\apache-tomcat-6.0.29\lib\servlet-api.jar;G:\springwork\server\apache-tomcat-6.0.29\lib\jsp-api.jar;.;
When I go to bin folder and double click on startup.bat then my tomcat starts and when I double click on shutdown.bat tomcat stops.
But I want using CMD start and stop the tomcat.
And in any folder I write command startup.bat the server will start and when I write shutdown.bat the server will stop.
Add %CATALINA_HOME%/bin to path system variable.
Go to Environment Variables screen under System Variables there will be a Path variable edit the variable and add ;%CATALINA_HOME%\bin to the variable then click OK to save the changes. Close all opened command prompts then open a new command prompt and try to use the command startup.bat.
Steps to start Apache Tomcat using cmd:
1. Firstly check that the JRE_HOME or JAVA_HOME is a variable available in environment variables.(If it is not create a new variable JRE_HOME or JAVA_HOME)
2. Goto cmd and change your working directory to bin path where apache is installed (or extracted).
3. Type Command -> catalina.bat start to start the server.
4. Type Command -> catalina.bat stop to stop the server.
This is what I used to start and stop tomcat 7.0.29, using ant 1.8.2. Works fine for me, but leaves the control in the started server window. I have not tried it yet, but I think if I change the "/K" in the startup sequence to "/C", it may not even do that.
<target name="tomcat-stop">
<exec dir="${appserver.home}/bin" executable="cmd">
<arg line="/C start cmd.exe /C shutdown.bat"/>
</exec>
</target>
<target name="tomcat-start" depends="tomcat-stop" >
<exec dir="${appserver.home}/bin" executable="cmd">
<arg line="/K start cmd.exe /C startup.bat"/>
</exec>
</target>
you can use this trick to run tomcat using cmd and directly by tomcat bin folder.
1.
set the path of jdk.
2.
To set path.
go to Desktop and right click on computer icon.
Click the Properties
go to Advance System Settings.
then Click Advance to Environment variables.
Click new and set path AS,
in the column Variable name=JAVA_HOME
Variable Value=C:\Program Files\Java\jdk1.6.0_19
Click ok ok.
now path is stetted.
3.
Go to tomcat folder where you installed the tomcat.
go to bin folder.
there are two window batch files.
1.Startup
2.Shutdown.
By using cmd
if you installed the tomcate in D Drive
type on cmd screen
D:
Cd tomcat\bin
then type
Startup.
4.
By clicking them you can start and stop the tomcat.
5.
Final step.
if you start and want to check it.
open a Browser in URL bar type.
**HTTP://localhost:8080/**
Change directory to tomcat/bin directory in cmd prompt
cd C:\Program Files\Apache Software Foundation\Tomcat 8.0\bin
Run the below command to start:
On Linux: >startup.sh
On Windows: >startup.bat
Run these commands to stop
On Linux: shutdown.sh
On Windows: shutdown.bat
Create a .bat file and write two commands:
cd C:\ Path to your tomcat directory \ bin
startup.bat
Now on double-click, Tomcat server will start.
I have just downloaded Tomcat and want to stop it (Windows).
To stop tomcat
run cmd as administrator (I used Cmder)
find process ID
tasklist /fi "Imagename eq tomcat*"
C:\Users\Admin
tasklist /fi "Imagename eq tomcat*"
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
Tomcat8aaw.exe 6376 Console 1 7,300 K
Tomcat8aa.exe 5352 Services 0 124,748 K
stop prosess with pid 6376
C:\Users\Admin
taskkill /f /pid 6376
SUCCESS: The process with PID 6376 has been terminated.
stop process with pid 5352
C:\Users\Admin
taskkill /f /pid 5352
SUCCESS: The process with PID 5352 has been terminated.
There are multiple ways to Start and Stop Apache Tomcat Server in Linux and Windows Operating systems. Below is detail facts. Locate the bin folder in your tomcat server and execute the following commands in CMD/ Terminal.
Linux:
./catalina.sh run
Passing "run" argument for catalina.sh --> starts the Tomcat in the foreground and displays the running logs in the same console. when the console terminal is closed it will terminate the tomcat.
./catalina.sh start | ./catalina.sh stop
Passing "start" argument for catalina.sh --> starts the Tomcat in the background. Since in background no issues closing down the terminal. The logs need to be viewed as below: tail -f $CATALINA_HOME/logs/catalina.out
./startup.sh | ./shutdown.sh
The last way is firing the startup.sh to start your Tomcat server. If you Vi the script you can see it calls catalina.sh script passing start as the argument. This will be running in background as well.
Windows:
startup.bat // start tomcat server
shutdown.bat // stop tomcat server
You can use the following command
c:\path of you tomcat directory\bin>catalina run

Categories