I have a .bat file which is my .jar file launcher. The .jar file is a native application that talks with a Chrome extension. I have the following .bat file:
#echo off
rem set JARFILE = %CD%\MyNativeApp.jar
java -jar C:\Users\reza.ahmadi\workspace\SimpleNativeApp\bin\MyNativeApp.jar
pause
The .jar and the .bat files are in the same folder (along with my manifest). It works well.
My problem: It does not work if I replace that path there with just the file name, meaning:
java -jar MyNativeApp.jar
does not work for me. I am working with a Win 8 machine.
Any comments would be appreciated.
You can dynamically substitute the path by getting it from the currently executing batch file (%~dp0):
java -jar "%~dp0MyNativeApp.jar"
If the jar file is in the same folder that the batch file, you can use
#echo off
setlocal enableextensions disabledelayedexpansion
for %%a in ("%~dp0\MyNativeApp.jar") do set "JARFILE=%%~fa"
java -jar "%JARFILE%"
pause
You can find here more information on how to retrieve the current active folder or the folder where the batch file is stored
Related
I have a runnable jar-file which I want to start from a batch file. However the jar-file has to be started with a VM-option. The following batch file starts the jar file (in a static way).
java -Djava.security.policy=C:\Users\uname\
\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://C:/Users/uname/Documents/Folder
/anotherFolder/target/classes/ -jar %~dp0jarfile.jar %*
pause
btw: I know that
\src\main\java\rmi\client.policy
is not in a jar file yet, but I'm assuming that everybody has this file structure already on his machine.
However, I want to be able to start the jar file with a relative path, so that every Windows10 (x64) user can use my jar file system-independently. How to achieve that via batch?
Replace each reference to user home C:\Users\... with %userprofile% variable as per this answer explanation.
java -Djava.security.policy=%userprofile%\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://%userprofile%/Documents/Folder/anotherFolder/target/classes/
-jar %~dp0teamFour-1.0-SNAPSHOT.jar %*
or switch to %userprofile% directory with cd before executing java and depend on relative paths.
I can't open .jar file on Windows 10 (I have tried everything like reinstalling Java and setting the default app to open it to be javaw.exe) and I don't exactly trust jarunner/jarfixer so I want to make a batch program which would detect whenever a .jar is being opened it would get the path of it and would run java -jar [pathtofile].
You'll need to execute this as administrator:
ASSOC .jar=jarfile
FTYPE jarfile=c:\java\java.exe -jar "%1" %*
setx PATHEXT "%PATHEXT%;.jar"
Just put the correct path to the java.exe . Mind that you'll have to keep all your dependencies in the %classpath% vatiable.
More for ASSOC and FTYPE commands.
I am not sure if this is related, but I used this tool http://launch4j.sourceforge.net/ to make .exe from .jar
I made a batch file that executes commands through a jar:
#java -jar commands.jar %*
This is the file tree:
C:\
Commands\
commands.bat
commands.jar
I have my PATH variable set to "C:\Commands\" so that I can access the commands from anywhere on the PC. I can successfully run the batch file, but for some reason it won't open the jar. When I type
commands
or
commands help
it says
Error: unable to access jarfile commands.jar
I have tried using quotes around the jar name, I have tried moving the jar... nothing works...
%~dp0 gives bat file path so
#java -jar %~dp0commands.jar %*
See call /? for help (even though we aren't using call).
By using ftype, assoc, and editing with setx the variable pathext to add jar files you can just type the jar's name and have it run without a batch file needed.
I have a jar file named test.jar, which I am running with a batch script from the same folder.
Here's the batch code:
java -jar test.jar
pause
The jar itself works with no problems, and I can run it just fine.
However, if I try to run the batch file as an administrator (by right clicking it and choosing "Run as Administrator"), I get the following error:
Error: Unable to access jarfile test.jar
I'm using Windows 8.1, but this also happened on a machine running Windows 7.
What can I do to be able to run this as an Administrator?
i had the same problem that you and i solved it by changing
java -jar test.jar
to
java -jar %~dp0test.jar
%~dp0 holds the directory of your bat file (AFAIK) so %~dp0 will give Java the full path to the jar file solving the problem.
You could also ad a temp path to java
path=C:\Program Files\Java\jre1.8.0_40\bin
Java script
path=
I can't believe this is difficult. But all the stuff I'm reading on Microsoft sites says to run the batch file minimized - which is ridiculous, or launch via VB script???? I have to run a VB script to launch a batch file??? This is insanity.
Run the javaw executable, not java.
Use jsmooth to create an exe. http://jsmooth.sourceforge.net/. It will also detect the location of Java and if the user doesn't have it installed offer to download it for the user.
If you don't mind it flashing the black window when you run the .bat file use javaw -jar myapp.jar in your batch file.
#echo off
cd C:/link
javaw -jar jarfile.jar
Use this for your batch file.
Perhaps making it a exe file by: http://viralpatel.net/blogs/convert-jar-to-exe-executable-jar-file-to-exe-converting/
Or by doing this:
#echo off
start /B server.jar [arg1] [arg2]
start /B server.jar [arg3] [arg4]
#echo on
Found here: Launch .jar files with command line arguments (but with no console window)