Cao Felix
Cao Felix

Reputation: 329

Use Windows Batch File to start a Java program

I am not familiar with batch script, but I want to create a Windows Batch File to start a Java program. The problem is that it has to specific the path where JRE is installed. When you install both JRE7 and JRE8, the name of that JRE8 folder would call something like jre1.8.0_20 or jre1.8.0_40 with the version number in the back. When you have only JRE8 installed, the folder would call jre8. Is there an easier way to find where the most updated JRE installed and then execute it? Thanks.

start ..\..\Java\jre7\bin\javaw.exe -Xms512M -Xmx1024M -Djna.library.path=.\lib -cp example.jar; com.example.main

Upvotes: 0

Views: 2447

Answers (2)

Cyclonecode
Cyclonecode

Reputation: 29991

You should be able to get the location of javaw.exe by executing where java. This can be set as a variable inside a batch file like this:

# sets a variable called 'java' to the location of javaw.exe
for /f "delims=" %a in ('where javaw') do @set java=%a
# execute you jar file
%java% -jar <app.jar>

Noticed that the above only seems to work when running directly from the command line. Here is another example that should work in a batch file:

# run.bat
@echo off
setlocal enabledelayedexpansion
for /f %%a in ('where javaw') do (
  set java=%%a
)
!java! -jar %1

The above batch file should be called with the name of the jar file:

 run.bat app.jar

Upvotes: 1

ventsyv
ventsyv

Reputation: 3532

I think it's best to just user JAVA_HOME and/or JRE_HOME and let the user / sysadmin worry what's installed.

Upvotes: 0

Related Questions