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.
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.
Related
[(It's sad this question has already been asked.)]
I've the batchfile D:\Hydroper\Projects\Java\ASC\bin\asc.cmd that executes a JAR file .\target\asc-1.jar in the same directory. Absolute path: D:\Hydroper\Projects\Java\ASC\bin\target\asc-1.jar.
However, when I execute my batchfile, I get the Unable to access jarfile error:
asc -strict Main.as
but my JAR is there. Even adding a .jar or executing with admin privileges the error persists. It was automatically built with Maven.
Here my batch:
java -jar "D:\Hydroper\Projects\Java\ASC\bin\target\asc-1" %*
It works when I do java -jar ... args manually in the command-line, but I don't like that. I prefer aliasing that command.
ClassPath
I've instead also tried something like:
java macromedia.asc.embedding.Main "%*"
It works fine, but ASC doesn't. asc -help works fine, but asc -help Main.as says my AS file cannot be found, but I'm in the right directory.
Simply do the below:-
cd D:\Hydroper\Projects\Java\ASC\bin
copy the asc.cmd file to bin folder and modify the batch file like below
java -jar target\asc-1.jar
First execute the above command from command prompt first before executing the same from batch file like below:-
cd D:\Hydroper\Projects\Java\ASC\bin
java -jar target\asc-1.jar
When we use a batch file or shell script to run a java application do it like below:-
in your batch file
CP="C:\lib\example1.jar;C:\lib\example2.jar;D:\Hydroper\Projects\Java\ASC\bin\target\asc-1.jar"
java -cp %CP% com.your.java.class.MyExample
Suppose I have some folder in a directory:
- MyApp
- lib
- myapp.jar
The location of the MyApp directory is supposed to be stored in an environment variable, like APP_HOME. I would like to add a bin folder that contains two commmand-line executables that launch the java program, one for Windows and one for Unix-based OSs. I already know that one file would just be called myapp and modified with chmod +x, and the Windows one would be named myapp.bat.
What I am unsure about is what the contents of these files would be. As said, both would run the jar file with a custom command line command whose arguments are passed to the main method, as shown below:
>myapp -debug key=value moreargs...
EDIT: How would I go about creating this environment variable, from Java code?
You can pass command line arguments to the executable by adding $* at the end of the command in the Unix shell script, and %* for the Windows batch file:
java -jar $APP_HOME/lib/myapp.jar $*
After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist
WHY is it that when executing, from command prompt,
c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar
it does NOT work (i get 'unable to access jarfile Addition.jar)
BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine
The -classpath argument is ignored when you use the -jar option. See the documentation.
because java doesn't look in classpath to launch jar file for this command it needs file as input
so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid
you can give full path to jar like from any directory
java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar
I can run java in cygwin+windows using the following settings (the sw/jar directory has several jar files, and I pick the relevant one from the java command line):
CLASSPATH=.;C:\sw\java_6u35\lib\\*;C:\sw\jar\\*
java org.antlr.Tool Calc.g
But I am having the following problems when running in linux:
(1) I can't set a directory name in a classpath, the following line reports an error:
setenv CLASSPATH .:/sw/jdk1.6.0_35/lib/\*:/sw/jar/*
(2) when I run explictly with -jar option, I still get an error:
java -jar /sw/jar/antlr-3.4.jar org.antlr.Tool Calc.g
error(7): cannot find or open file: org.antlr.Tool
However, the class does exist. When I do jar tf /sw/jar/antlr-3.4.jar, I get:
...
org/antlr/Tool.class
So my question is: (a) how do I specify in unix that my jar-directory is xxx that contains several jar files, and (2) how do I pick the relevant jar from this dir at runtime?
To specify multiple jars in a directory, directly in the java command, use this
java -cp "/sw/jar/*" org.antlr.Tool Calc.g
This will include all the jars
If you want to set the classpath in Unix/Linux systems, use this
export CLASSPATH=/sw/jar/a.jar:/sw/jar/b.jar
in unix use this to set the classpath:
export CLASSPATH=myClassPath
about not finding your jar, you're using a leading slash (/), that means that you path is absolute (not relative to your home folder) is this what you want?
if you want the path to be relative to your folder try:
java -jar ~/mypathToMyJar
I have placed the clojure-1.4.0.jar path (C:\clojure-1.4.0\clojure-1.4.0.jar) in my CLASSPATH environment variable. Now when I try to launch the REPL from the command line with the following code:
java -cp clojure-1.4.0.jar clojure.main
I get an error:
Error: Could not find or load main class clojure.main
It used to work before I set up emacs. Any ideas.
You can either add clojure jar file to CLASSPATH environment variable:
/some/where % CLASSPATH=/tmp/clojure-1.4.0.jar java clojure.main
or specify it directly in java arguments:
/some/where % java -cp /tmp/clojure-1.4.0.jar clojure.main
Setting CLASSPATH variable and providing -cp argument to java command at the same time is pointless, because -cp argument overrides CLASSPATH completely. This is the cause of your problem: you seem to be invoking java command not from the directory where clojure-1.4.0.jar is located, so -cp clojure-1.4.0.jar switch makes java program try to locate clojure-1.4.0.jar in the current directory and ignore CLASSPATH. Since there is no clojure-1.4.0.jar in the current directory, the command fails.