locating jar file in $PATH environment variable for java -jar command
I want to run a simple executable jar like this:
java -jar /path/to/my/jar/myjar.jar
However, i have to put that absolute path every time I want to run it. Is there a way to开发者_运维百科 tell the OS to look for the jar in $PATH
environment variable?
You can also copy your jar file to /usr/bin folder, then it will work with just $ myjar.jar
Make sure your jar file has the executable bit (chmod +x myjar.jar
) and copy it to /usr/bin (sudo cp myjar.jar /usr/bin/myjar.jar
)
I think you'd use the classpath; append your directory to it and java -jar myjar.jar
should work.
You could use CLASSPATH environment variable instead
The -jar directive overrides the classpath, using whatever is defined in the jar file itself. The best way to do what you need is to use a tool such as ant, a script, or copy it to somewhere where the OS can find it (as Nejc Saje suggested).
精彩评论