Batch file to set java version without installing - java

I saw many similar questions but didn't get help I needed.
My requirement is to find the Java version 8 from the system folders and call a jar file without installing Java 8. No need to set JAVA_HOME and there are multiple versions of Java available in the system folders.
I only needed to check the Major version of Java for my requirement.
I wrote something like
#echo off
call "%~dp0..\bin\java.exe"
d:
cd JARFOLDER
java -jar test.jar
But I dont know to check the major version of java.
Update:
I used both the solutions given by #Mofi and used for my requirement as below and it is working fine.
#echo off
rem Get path of latest installed Java directly from Windows registry.
for /F "skip=1 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe" /v Path 2^>nul') do if /I "%%N" == "Path" set "JAVA_8_HOME=%%P" & goto JavaPathFound
rem Path of Java not found in registry, search for 32-bit Java in the default
rem program files folders of 64-bit and 32-bit Windows and take first found.
if "%ProgramFiles(x86)%" == "" goto Windows_x86
for /R "%ProgramFiles(x86)%" %%I in (java*.exe) do set "JAVA_8_HOME=%%~dpI" & goto JavaPathFound
:Windows_x86
for /R "%ProgramFiles%" %%I in (java*.exe) do set "JAVA_8_HOME=%%~dpI" & goto JavaPathFound
echo Error: Java binary directory not found.
echo/
pause
goto :EOF
:JavaPathFound
if not "%JAVA_8_HOME:~-1%" == "\" set "JAVA_8_HOME=%JAVA_8_HOME%\"
set "JAVA_8_HOME=%JAVA_8_HOME%java.exe"
for /F "tokens=1-3" %%A in ('"%JAVA_8_HOME%" -version 2^>^&1') do (
if /I "%%A %%B" == "java version" (
set "JavaVersion=%%~C"
goto EvaluateVersion
)
)
echo Error: Failed to determine version of installed Java.
goto :EOF
:EvaluateVersion
echo Java version is: %JavaVersion%
for /F "tokens=2 delims=." %%I in ("%JavaVersion%") do set "jver=%%I"
echo Main version is: %jver%
echo Path is: %JAVA_8_HOME%
if %jver%==8 (
echo "The processor will be started with Java 8"
cd /D D:\JARFOLDER
"%JAVA_8_HOME%" -jar test.jar
)
endlocal
Thanks in Advance.

Yes, you can do so by reading the release file present inside JRE folder of each of your JAVA folder inside SYSTEMS folder.
It resides in directory C:\Program Files\Java\jre7\ so in your case it may be somewhat like this ...\SYSTEMS\Java\jre. Your script should be capable of reading this file from each of your JAVA or JRE folders and then try to find the biggest value.

Related

Different java version requested

From this repository
I try to run the following step
Download Mairesse's tool PersonalityRecognizer and unzip it to the
same location. Check the installation instructions to properly
configure the tool.
However I receive this error:
$ ./PersonalityRecognizer -i ../output_dir -d -t 2 -a ../mairesse_Apache.arff
./PersonalityRecognizer: line 15: ../../apps/jdk1.5.0_05/bin/java: No such file or directory
In this file, which is from the tool, I setted the version of java I have
#echo off
rem WINDOWS LAUNCH SCRIPT
rem ENVIRONMENT VARIABLES TO MODIFY
set JDK_PATH="C:\Program Files\Java\jdk1.8.0_151"
set WEKA="H:\apps\weka-3-4\weka.jar"
rem ----------------------------------
set COMMONS_CLI="lib\commons-cli-1.0.jar"
set JMRC="lib\jmrc.jar"
set LIBS=%WEKA%;%COMMONS_CLI%;%JMRC%;%CD%;bin\
%JDK_PATH%\bin\java -Xmx512m -classpath %LIBS% recognizer.PersonalityRecognizer %1 %2 %3 %4 %5 %6 %7 %8 %9
Why the error shows a different version and path? What can I do?
if I type in cmd the followning I see a different path.
C:\Users\myuser>which java
/c/ProgramData/Oracle/Java/javapath/java
How can I change it which jdk if this is the option to fix the problem?

.jar UI not working in Linux redhat/centos 7.X

I have a JAR file which is used to convert txt file to PDF. I am not sure on which platform it was build.
While running this jar in linux, the UI is not visible.
Jar is working properly in windows
There are 2 files, jar and bat file; bat file is a trigger.
You can download the file here: https://drive.google.com/open?id=1bnNVMdYwCLUeYSB7RH1oi29ygvmH9HCa
and please post your suggestions.
What I have tried,
This utility requires, java 1.6+; I have tried java 1.7 and 1.8 --- did not work
Changed java classpath to “.:jarfile:bin_path” --- did not work
Tried –verbose option, no failed component found
$JAVA_HOME AND $CLASSPATH ARE FINE
Tried: java –Xmx1024m –jar jarfile.jar –did not work
One batch file is attached with utility, which is to trigger jar file; it contains javaw command which is not available in linux. So I am using java – jar command.
bat file contains :
#echo off
set CHK_JAVA=""
set TEMP_FILE=%TEMP%\javaCheck%RANDOM%%TIME:~9,5%.txt
if %TEMP_FILE%=="" (
set TEMP_FILE=C:\javaCheck%RANDOM%%TIME:~9,5%.txt
)
echo TRACES PDF Generation Utility to generate Form 16 / 16A requires JAVA Version 1.6 or above.
java -version 2>%TEMP_FILE%
FOR /F "tokens=*" %%i in (%TEMP_FILE%) do (
echo %%i | find "version" >nul
if not errorlevel 1 (
echo %%i | find "1.6" >nul
if not errorlevel 1 (
set CHK_JAVA="OK"
)
echo %%i | find "1.7" >nul
if not errorlevel 1 (
set CHK_JAVA="OK"
)
echo %%i | find "1.8" >nul
if not errorlevel 1 (
set CHK_JAVA="OK"
)
)
)
del %TEMP_FILE%
if %CHK_JAVA%=="OK" (
echo Launching now
rem echo %~dp0TRACES-PDF-CONVERTER.jar
rem pause
start javaw -Xms1024m -jar %~dp0TRACES-PDF-CONVERTERV1.3L.jar
) else (
echo You do not have JRE version 1.6 or above required to launch TRACES PDF Generation Utility.
echo Please install JRE version 1.6 or above and try again.
echo Visit http://www.java.com/en/download/index.jsp for JRE download details.
pause
)

Reliable way to find JRE installation in Windows bat file to run java program

When installing the latest JRE 7 on Windows, it no longer adds the command java to the system path. So just calling java --version in bat file fails in that case (despite that Java from java.com is installed).
What's a reliable way to find the java command installation directory in a windows bat file?
I've seen it in the following locations:
C:\Program Files\Java\jre6\bin\java.exe
C:\Program Files\Java\jre7\bin\java.exe
C:\Program Files (x86)\Java\jre7\bin\java.exe
Not tried JRE 8 yet.
Note: if there are multiple, I 'd like the default (or the latest - I don't care).
If it is installed, ask windows where it is
#echo off
setlocal enableextensions disabledelayedexpansion
rem Where to find java information in registry
set "javaKey=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
rem Get current java version
set "javaVersion="
for /f "tokens=3" %%v in ('reg query "%javaKey%" /v "CurrentVersion" 2^>nul') do set "javaVersion=%%v"
rem Test if a java version has been found
if not defined javaVersion (
echo Java version not found
goto endProcess
)
rem Get java home for current java version
set "javaDir="
for /f "tokens=2,*" %%d in ('reg query "%javaKey%\%javaVersion%" /v "JavaHome" 2^>nul') do set "javaDir=%%e"
if not defined javaDir (
echo Java directory not found
) else (
echo JAVA_HOME : %javaDir%
)
:endProcess
endlocal
This is how to find them.
Dir c:\java.exe /a/s
searches entire drive.
You can put it in a for loop. (in for loops %A becomes %%A ina batch)
#for /f "tokens=1-8 delims=/ " %A in ('dir "c:\program files\java.exe" /a /s^|findstr /i /v /r "DIR VOL \(s\) Listed"') do echo day %A month %B Year %C Name %G
This finds it and puts day month year into seperate variables.
For /?
dir /?
findstr /?

DOS script to check if the default java installed version is greater than 1.x

As the subject said, i need a dos script to check the version of java installed on windows xp Machine.
Furthermore, I need to check if the version is greater than a prefixed value 1.x.
Anyone can help me?
Thanks!
Getting the version, and write it into a temp file. Then only parse the version itself:
#echo off
echo off
java -version 2> tmp_java_version.txt
set /p JAVA_VERSION= < tmp_java_version.txt
del tmp_java_version.txt
set JAVA_VERSION=%JAVA_VERSION:~14,3%
echo %JAVA_VERSION%
pause > NUL
if you can download and gawk for windows.
C:\test>java -version 2>&1 | gawk "NR==1{print $3}"
"1.6.0_16"
java -version
You can also use the command
java -fullversion
and produce output such as:
java full version "1.6.0_17-b04"
On a computer without any version of Java from Sun Microsystems installed, this results in an error message:
'java' is not recognized as an internal or external command, operable program or batch file.
In a batch you could do:
#echo off
java.exe -fullversion 2> c:\temp\out.txt
for /F "tokens=4" %%i IN (c:\temp\out.txt) DO echo %%i
You can't avoid the temp file because the java.exe output writes on the standard error! So you have to redirect the standard error to a file.
In commmand prompt you can just type it has java -version.
For example:
D:\Users> java -version
then you will get the answer as
java version 1.8.0_11

JAVA_HOME directory in Linux

Is there any linux command I could use to find out JAVA_HOME directory? I've tried print out the environment variables ("env") but I can't find the directory.
On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))
On Mac you can run $(dirname $(readlink $(which javac)))/java_home
I'm not sure about windows but I imagine where javac would get you pretty close
Just another solution, this one's cross platform (uses java), and points you to the location of the jre.
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
Outputs all of java's current settings, and finds the one called java.home.
For windows, you can go with findstr instead of grep.
java -XshowSettings:properties -version 2>&1 | findstr "java.home"
echo $JAVA_HOME will print the value if it's set. However, if you didn't set it manually in your startup scripts, it probably isn't set.
If you try which java and it doesn't find anything, Java may not be installed on your machine, or at least isn't in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn't be used to determine if java is installed or not.
I know this is late, but this command searches the /usr/ directory to find java for you
sudo find /usr/ -name *jdk
Results to
/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk
FYI, if you are on a Mac, currently JAVA_HOME is located at
/System/Library/Frameworks/JavaVM.framework/Home
To show the value of an environment variable you use:
echo $VARIABLE
so in your case will be:
echo $JAVA_HOME
In case you don't have it setted, you can add in your .bashrc file:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
and it will dynamically change when you update your packages.
If $JAVA_HOME is defined in your environment...
$ echo $JAVA_HOME
$ # I am not lucky...
You can guess it from the classes that are loaded.
$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]
This method ensures you find the correct jdk/jre used in case there are multiple installations.
Or using strace:
$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
On the Terminal, type:
echo "$JAVA_HOME"
If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using "locate java" to try and discover where your installation of Java is located.
Did you set your JAVA_HOME
Korn and bash shells:export JAVA_HOME=jdk-install-dir
Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
C shell:setenv JAVA_HOME jdk-install-dir
Here's an improvement, grabbing just the directory to stdout:
java -XshowSettings:properties -version 2>&1 \
| sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'
You can check from the command line by executing this command echo $JAVA_HOME. If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives --config java which lists all installed versions with current active one marked and provides dialog to switch:
There are 3 programs which provide &apos;java&apos;.
Selection Command
-----------------------------------------------
1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java)
2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java)
*+ 3 /usr/java/jdk-17.0.2/bin/java
Enter to keep the current selection[+], or type selection number:
from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2
http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment
If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.

Categories

Resources