Run Jar Files in PATH

Wassadamo :

I store jar files in C:\Users\myuser\javatools\avro-tools

And added them to my PATH:

echo %PATH%
...;
C:\Users\myuser\javatools\avro-tools;

I can run them by specifying the full path:

java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar

But cannot run them without the full path:

java -jar avro-tools-1.8.1.jar
Error: Unable to access jarfile avro-tools-1.8.1.jar

I need to run jar files without changing to the directory, nor specifying these full paths.

Andreas :

UPDATE: Added %*

I'd recommend creating a batch file and run that instead.

avro-tools-1.8.1.bat

@echo off
java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*

Place .bat file somewhere in PATH, and run by simply typing:

avro-tools-1.8.1.bat -abc def

The %* in the .bat file gets replaced with any argument passed to the .bat file, so the -abc def arguments are passed to the avro-tools program in the args array to the main method.


If you have multiple versions of Java installed, you can then choose which one to use when running that .jar file, by also qualifying the java command.

avro-tools-1.8.1.bat

@echo off
"C:\Program Files\Java\jdk1.8.0_181\java.exe" -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*

Now that code will run with Java 8, even if Java 8 is not the default Java on your machine.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216650&siteId=1