How to test that a Jar is in a classpath in windows? - java

I have the classic :
java.lang.NoClassDefFoundError: org/python/util/jython
Even if the jar jython.jar exist in the environment variable PATH for my user and system wide path.
How can I quickly test that I am not crazy and it is indeed in the path?

Java doesn't use the PATH variable.
To specify the classpath when running a java application use the -cp parameter for the java command.
Inside your batch file (the .cmd file) find the java command and add the needed jar file:
java -cp somefile.jar;\path\to\jython.jar someclass.MainMethod
Please don't use the deprecated CLASSPATH any more.
For more details, please see the Java documentation:
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html#tooloption

use the following command
set classpath="path to your jar/jython.jar";

Related

java 'jar' is not recognized as an internal or external command

I'm getting the following error when I try to run the 'jar' command in the command line on windows :
'jar' is not recognized as an internal or external command
The general solution seems to be that I need to add the jdk bin folder to the PATH in environment variables. I have already done this, I added the following to the path variable :
...; C:\Program Files\Java\jdk1.8.0_40\bin\;
Though I'm not sure if having the jdk reside in 'program files' instead of 'program files x86' affects this. I'm on 64 bit windows with 64 bit java.
Thanks
The path should only contain directories where the OS will look for executables. Strip the trailing "\jar.exe" to set your path as:
(old path variables here);C:\Program Files (x86)\Java\jdk1.7\bin
Thanks To : #stevevls
Try using this command:
java -version
If it doesn't work that means you failed to properly add path to jdk.
If it does work, try using:
java -jar
The jar command in command line is used in order to create a JAR file. For example:
jar cf jar-file input-file(s)
See more at: Oracle docs
If you want to run the existed JAR file you should use the java -jar command mentioned by #Aleksandr.
But in your case it looks like you don't have an access to the added directory in your path with JKD binaries so at the beginning try to execute mentioned java --version to check whether you have java in you classpath at all.
Please remember that on Windows you have to restart the console when you change the windows default path. You do not see the changes on the current console.
The x64 version of Java is installed on Program Files. The x86 version is installed on Program Files x86 by default.
Five steps to solve this problem:
check whether in your bin folder jar.exe is availbale or not
check for your environmental setting
download jar file from here http://originaldll.com/file/jar.exe/29248.html and keep in bin folder
now check for jar it will 100% work
keep any jar in your desktop and go to desktop path on cmd prompt and run the command jar xf filename.jar
Now see if it is working

How does java locate a JAR file?

The JAR file locates at C:\Workbench\jars\antlr-4.4-complete.jar
The environment variables are:
CLASSPATH=.;C:\Workbench\jars\*
PATH=C:\Workbench\jars;...
I am trying with the following commands:
java -jar "C:\Workbench\jars\antlr-4.4-complete.jar" <-- OK
java -jar antlr-4.4-complete.jar <-- FAIL!
java org.antlr.v4.Tool <-- OK
I am totally confused about the failed one. I am expecting the PATH variable will be looped through to locate the jar file. But it seems not. Why?
My guess is, the implementation of java -jar command line doesn't use the PATH variable for searching jar file. But still, why?
The PATH is for looking up the command to execute, in this case java will be looked up on the PATH.
You will need to supply either an absolute or relative path to java -jar because the terminal (bash/windows/zsh/etc...) will not expand arguments in this way. CLASSPATH is used by Java to look up further jars, but it expects a correct path to the initial jar as the first argument.

Run java program sing Cmd

I'm trying to run Java program using cmd.
When I compile that file using
javac helloworld.java
It works perfectly but when I try to run that file using
java helloworld
I get an error:
couldn't find or load main class.
even though my I put my javac path in class path in system variables and javac working correctly.
After searching on how I can fix it I found that I can use
java -cp . helloworld
because it let you to find .class file.
and it works but I know that I can run java program without this -cp so what this for and how I can run my program without it?
-cp specifies the Java classpath for the JRE you are attempting to start. Look for an environment variable CLASSPATH and add '.'.
-cp is used to set the classpath for the jar file, this flag is same as importing a jar file to eclipse and then use it.
If you want to run without this flag, use should set the classpath first beforing running.
export CLASSPATH=path/to/your/jarfile.jar
If you already have some classpath set
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar
If you want to include current directory
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar:.
As other have mentioned, you can set the CLASSPATH. However, a much better approach is to bundle the .class files in a JAR ans user java -jar nameofthejar.jar.

Why do I have to use java -cp . (class name) and not java (class name)?

I was just curious why on some systems like mine, I have to utilize java -cp . and simply using the java command in the terminal window doesn't work ?
When you are launching a JVM using the java command, the JVM's classpath is determined as follows:
If you use the "-jar" option, then the classpath consists of the JAR file itself, and together with the optional "Classpath" attribute in the JAR file.
Otherwise, if you use the "-cp" option, the option's value gives the classpath
Otherwise, if the CLASSPATH environment variable is set, then that gives the classpath
Otherwise, the classpath consists of just the current directory; i.e. ".".
Now you say that you have to explicitly give "-cp ." in order for the java command to execute your commands correctly.
The most likely explanation is that you have the CLASSPATH environment variable set to something inappropriate. When you run a java MyClass, it will be looking on the classpath specified by CLASSPATH ... and failing. But when you add "-cp .", you are saying "ignore CLASSPATH and just look in the current directory".
The option -cp is used to add a path to a directory or files that the Java Environment will load for only that execution. Those files "will contains the references to the libraries" you use in your program.
Alternatively to use the -cp you can set the classpath permanently. The way of setting it depends on the operating system you use.
More information here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Why javac can compile in this case

My environment is Windows 7 and JDK 1.7.
I have not set the CLASSPATH environment variable; echo %CLASSPATH% outputs nothing.
Java compiler: C:\Program Files\Java\jdk1.7.0_10\bin
Java source: is in D:\tmpmulu\Tj.java
I run the command like below:
C:\>"C:\Program Files\Java\jdk1.7.0_10\bin\javac.exe" -cp d:\tmpmulu\ d:\tmpmulu\Tj.java
It works. The command set the classpath and compiled the file.
But when I change the command to use . instead of d:\tmpmulu\ as my classpath:
C:\>"C:\Program Files\Java\jdk1.7.0_10\bin\javac.exe" -cp . d:\tmpmulu\Tj.java
It's also OK.
That confused me. The . means the current path, it should be c:\. How did it run successfully?
Another question is command like below:
C:\>"C:\Program Files\Java\jdk1.7.0_10\bin\javac.exe" -cp d:\tmpmulu\ Tj.java
As my thinking, the classpath is set to d:\tmpmulu\, it should find the Tj.java file. But the result is 'file not found Tj.java'.
Can anyone tell me details?
Well, presumably your code doesn't rely on having anything in the classpath, basically. If it only uses classes from the JDK, that's absolutely fine.
Note that the classpath is only used to find class files - not source code. That explains both the lack of failure when your source path isn't on the classpath, and then failure when you try to use the classpath to locate Tj.java.
First, if you're compiling with javac -cp . myClass.java, you can omit it the -cp . completely, since it's the default classpath.
Second, -cp should be used when you need to specify references to additional JARs file not included in the standard JDK library, such as a jdbc connector. If your class doesn't require any additional library, then it doesn't really matter what directory you tell javac to look into.

Categories

Resources