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
)
Related
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.
For the close everything crowd: This is not a duplicate of the question How to check if a process is running via a batch script because there may be multiple java.exe processes running (as they are in my case) and I need only to check for the specific jar. That's why I have written jar java application in the title.
I have a "Minecraft server" icon on my desktop that starts Spigot minecraft server. I use such run.bat file to do the job:
:start
java -Xmx1024M -Xms1024M -jar spigot-1.8.7.jar
pause
goto start
It's infinite loop to make server restarting simpler. Now it sometimes happens that I, or somebody else who's using the computer, try to start the server while it's already running. I would like to prevent this from happening. Something along:
IF spigot_server_running (
echo The server is already running. If you can't connect, try to check the console window for errors.
echo if all else fails, terminate the java process from task manager.
pause
exit 1
)
What can I do to detect it? I guess list of running processes can be parsed similarly to what Linux's grep does. But isn't there some command line tool for this distributed along with java JRE or JDK? I wouldn't install extra program for this task but I would prefer official Java API. But remember it must work with batch or Windows CScript files.
So I found out that there's this jps utility which conveniently lists running java processes and their arguments. I meantioned in the comments I think there's such a thing, but it took a while to dig out the proper use. I created following script:
#echo off
setlocal enabledelayedexpansion
echo Checking for %1
for /f "tokens=1" %%i in ('jps -m ^| find "%1"') do (
echo Jar already running
goto end
)
echo Jar not running
:end
pause
Usage:
> isjarrunning.bat something.jar
The name is case-sensitive!
In order to use this script in IF statements, it must be tweaked:
isjarrunning.bat
#echo off
IF [%1] == [] (
echo Invalid parameter. Usage:
echo isjarrunning.bat NameOfTheJarFile.jar
exit /B 2
)
for /f "tokens=1" %%i in ('jps -m ^| find "%1"') do (
exit /B 1
)
exit /B 0
Usage in your scripts:
testjarrunning.bat
#echo off
call isjarrunning.bat MyProgram.jar
echo Error level: %errorlevel%
if errorlevel 1 (
echo Jar file already running!
exit /b 1
) ELSE (
echo Starting jar file
start javaw -jar path\MyProgram.jar
)
echo Ende
So, here's the problem I encountered. I wrote a simple .bat file to run weka on some data sets I had but Java recently updated itself and it stopped working. My old code was this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
This worked before and it did the job I asked of it. However, after the java update, I kept getting the error "Could not find or load main class weka.classifiers.meta.FilteredClassifier". I couldn't figure it out because the directory names and class names were exactly correct. So, I changed the code to this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java -cp "C:\Program Files (x86)\Weka-3-6\weka.jar" weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
And it worked again. Can anyone tell me why this happened? The only thing I can think of is that the Java update isn't playing nice with itself somehow. Any insight would be appreciated thanks.
SET WEKA_HOME=c:\Program Files (x86)\Weka-3-6
SET CLASSPATH=%CLASPATH%;%WEKA_HOME%\weka.jar
bash learn.sh
I have a MyFile.jar file. I need a batch file that searches for the jar file and triggers it.
I found the following solution. But for that batch file and jar file must be in the same folder.
java -jar %~dp0MyFile.jar %*
We don't know where the client will place jar file in their system. But they will put batch file in startup folder.
Edit: I managed to write this batch file for now.
`ECHO
ECHO Locating the Jar
IF EXIST java -jar MyFile.jar ELSE (GOTO
SearchJar)
:SearchJar
SET the_path=D: & CD\
DIR /S/B File.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt
E: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt
C: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt
G: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt
H: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt
I: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt
ECHO Jar not found.
:FoundIt
ECHO Jar file found at %the_path%
CALL java -jar %the_path%`
This is working now in my system. But I am not sure if there will be a simpler way to check all the drives at once.
As gknicker and I commented above, this is the way it should be done. If you used an installer (like NSIS or similar) to deploy the .jar file and .bat script, have the installer write the registry value, and a search or file browse may never be needed. If the .jar file is moved after installation, pop up a GUI file chooser and let the user browse to the location he saved MyFile.jar. Save the updated location to the registry. Re-prompt if the file is moved again.
#echo off
setlocal enabledelayedexpansion
set "jarfile=MyFile.jar"
set "regkey=HKLM\Software\Company Name"
set "regvalue=jarfile"
set jarpath=
for /f "tokens=2*" %%I in ('reg query "%regkey%" /v "%regvalue%" 2^>NUL') do (
set "jarpath=%%~J"
)
if not exist "%jarpath%" (
call :chooser jarpath "%jarfile%" "%jarfile% (*.jar)|*.jar"
)
if exist "%jarpath%" (
reg add "%regkey%" /v "%regvalue%" /t REG_SZ /d "%jarpath%" /f 2>&1>NUL
) else (
<NUL set /P "=Unable to locate %jarfile%. This thread will self-destruct in 5 seconds... "
ping -n 6 0.0.0.0 >NUL
echo bang.
exit /b 1
)
java -jar "%jarpath%"
goto :EOF
:chooser <var_to_set> <filename> <filter>
:: based on https://stackoverflow.com/questions/15885132
setlocal enabledelayedexpansion
:: Does powershell.exe exist within %PATH%?
for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
set chooser=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.OpenFileDialog;$f.InitialDirectory='%cd%';$f.Filter='%~3';$f.showHelp=$true;$f.Title='Locate %~2';$f.ShowDialog()|Out-Null;$f.FileName"
) else (
rem :: If not, compose and link C# application to open file browser dialog
set chooser=%temp%\chooser.exe
>"%temp%\c.cs" echo using System;using System.Windows.Forms;
>>"%temp%\c.cs" echo class dummy{
>>"%temp%\c.cs" echo public static void Main^(^){
>>"%temp%\c.cs" echo OpenFileDialog f=new OpenFileDialog^(^);
>>"%temp%\c.cs" echo f.InitialDirectory=Environment.CurrentDirectory;
>>"%temp%\c.cs" echo f.Filter="%~3";
>>"%temp%\c.cs" echo f.ShowHelp=true;
>>"%temp%\c.cs" echo f.Title="Locate %~2";
>>"%temp%\c.cs" echo f.ShowDialog^(^);
>>"%temp%\c.cs" echo Console.Write^(f.FileName^);}}
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"
if not exist "!chooser!" (
echo Error: Please install .NET 2.0 or newer, or install PowerShell.
goto :EOF
)
)
:: capture choice to a variable
endlocal && for /f "delims=" %%I in ('%chooser%') do set "%~1=%%I"
:: Clean up the mess
del "%temp%\chooser.exe" 2>NUL
goto :EOF
If you insist on searching all local drives for MyFile.jar, you can query a drive list with wmic logicaldisk. Exclude network drives by adding where "not drivetype=4". You should still save the result to the registry to prevent a several minute delay on every run.
#echo off
setlocal
set "jarfile=MyFile.jar"
set "regkey=HKLM\Software\Company Name"
set "regvalue=jarfile"
set jarpath=
for /f "tokens=2*" %%I in ('reg query "%regkey%" /v "%regvalue%" 2^>NUL') do (
set "jarpath=%%~J"
)
if not exist "%jarpath%" (
call :finder jarpath "%jarfile%"
)
if exist "%jarpath%" (
reg add "%regkey%" /v "%regvalue%" /t REG_SZ /d "%jarpath%" /f 2>&1>NUL
) else (
<NUL set /P "=Unable to locate %jarfile%. This thread will self-destruct in 5 seconds... "
ping -n 6 0.0.0.0 >NUL
echo bang.
exit /b 1
)
java -jar "%jarpath%"
goto :EOF
:finder <var_to_set> <file_to_find>
setlocal
:: in wmic, drivetype=4 is network drive
for /f "tokens=2 delims=:=" %%I in (
'wmic logicaldisk where "not drivetype=4" get DeviceID /format:list ^| find "="'
) do (
<NUL set /P "=Searching %%I: for %~2... "
for /f "delims=" %%a in ('dir /s /b "%%I:\*%~2"') do (
echo %%~dpa
endlocal & set "%~1=%%a"
goto :EOF
)
echo Not found.
)
goto :EOF
First find the file and set a variable to its path, then use that variable as the path you want. From your use of %~dp0, I assume you are working on windows only, so the following example is for windows
echo off
for /r C:\search_folder_root %%a in (*) do if "%%~nxa"=="MyFile.jar" set jarLoc=%%~dpa
java -jar %jarLoc%MyFile.jar %*
Be warned - the search could take quite a long time e.g. if you search a whole drive...
EDIT:
In response to OPs comments requesting a script that will search any drive on the system, here it is. Please note - I don't think use of this is a good idea for user experience - it will be very inefficient and it could take ages... Tested on a very simple case with a dummy jar on a path that occurs early in the search on my machine. Note - if the user will know which drive the jar is on, or even better the path, you could always prompt them to enter that drive / path instead of looping through.
echo off
setlocal
set jarName=MyFile.jar
rem -- loop through each drive on system --
for /F "usebackq skip=1" %%i in (`wmic logicaldisk get caption 2^>NUL`) do (
echo Checking drive %%i for %jarName%
rem -- temporarily change to the drive --
pushd %%i\
rem -- recursively loop through every file of every subdirectory on that drive --
for /r %%a in (*) do (
if "%%~nxa"=="%jarName%" set jarLoc=%%~dpa
if defined jarLoc goto :Found
)
popd
)
rem -- if we get to here, the jar has not been found --
echo %jarName% not found on any drive in this system
goto :eof
:Found
echo Jar found at %jarLoc%
popd
java -jar %jarLoc%%jarName% %*
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 /?