I setup classpath in enviroment variables i set a user variable named classpath with value= .;C:\Users\Borut\Downloads\httpcomponents-client-4.1.2-bin\lib
the directory is full of .jar files. yet still when i run a program through cmd it reports an error that the package from the class does not exist.
Historically, you've needed to include the jar files themselves, not the directory they're in, on the CLASSPATH. In recent JREs, you can use a wildcard (i.e., a path ending in /*) to indicate all the jars in a directory. But if you indicate only a directory, then that directory is searched for class files and packages only; jar files are ignored.
You need to either (a) explicitly list the jar files, or (b) use a wildcard classpath (1.6+).
Related
Windows 10 1803 64-bit, Java JRE 8. No JDK installed.
I have a .jar in a folder:
c:\users\myuser\KickAssembler\KickAss.jar
Under System environment variables I have the CLASSPATH as:
c:\users\myuser\KickAssembler\
At a command prompt the CLASSPATH is set (checked with ECHO).
At a prompt when I try:
java -jar KickAss.jar
It returns:
Error: Unable to access jarfile KickAss.jar
If I CD into the directory first, then run it, it works fine. So it looks like CLASSPATH is not being used somehow. I have added the location into the standard Windows PATH also, still doesn't work.
Different combinations of casing for the filename doesn't work. Windows is set to show hidden files. It can't be a permissions issue because it works when I change into the directory first.
Anything else I can try?
CLASSPATH is used to define the path from which Java will load classes. CLASSPATH can contain folders and JAR files, such as: C:\mybin;c:\myjars\some.jar This classpath would load any fully qualified class file residing in or beneath c:\mybin and any fully qualified class within c:\myjars\some.jar. CLASSPATH will not allow JAVA to find your JAR file as you are expecting when using the -jar switch, it does not search for any JAR file along the classpath, it will only look in the ones explicitly stated in the CLASSPATH and then only for class files within them. Note, fully qualified means package + class, such as: com.myorg.somepackage.someclass, not just someclass.
As you observed, if you are in the folder where kickass.jar resides your command line works as the JAR file is present. If you fully reference the JAR file while executing the command from another folder the command line should work as well.
See https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html for more detail on how CLASSPATH works. It is important to have a solid understanding of CLASSPATH when using JAVA.
I have created a simple Java program (1 java file that contains the main() ), and I've included all Jar files in the same directory as the .class file. It is giving the NoClassDefFoundError message.
I've tried updating the Classpath to point to the directory, and I've also set "-cp ." to suggest that it look in the same directory as the .class file. However, the program still says it can't find the class def.
Any thoughts on what I should do?
Adding a folder tells java to look in that folder for .class files.
You can't reference .jar files via a folder name... Each .jar file's path must be listed on the CLASS_PATH explicitly.
this question's answer may be useful
When you try running a class from command line then a NoClassDefFound exception usualy means there is something wrong with your classpath.
You have explicitly define the classpath. You can do this in a few ways but the following way is the least prone to error:
Open a command shell and do the following:
1.) set classpath = {path to class files};{path to jars}
2.) java com.example.mainclass
Note: Even if your classes path and jar path is the same you need to specify them explicitly.
Note: If you have more then one jars place them in a folder say lib and add it to the classpath like: {path}/lib/* This will include all of the jar otherwise you have to specify them individually.
References: https://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Import the following package:
Import java.lang.NoClassDefFoundError;
my program is in C:\Users\Programs\x.java
X.java is using some files that are in y.jar, z.jar.
y.jar and z.jar are in C:\Users\Programs folder.
(1)
C:Users\Programs> javac x.java
(2)
C:Users\Programs> javac -classpath y.jar:z.jar x.java
I am not getting any errors when I do (2) but when I do (1) I am getting errors. Isn't that classpath is set to current folder. If so why is it not seeing y.jar and z.jar.
Contents of a jar residing on the classpath are not automatically added to the classpath itself. A proper classpath in your case would be what you specified in case (2).
The classpath includes the current folder.
However, it does not include subfolders of the current folder.
If you try to use com.example.MyClass, Java will look for a file named com/example/MyClass.class directly inside of each folder in the classpath.
It does not look in subfolders or JARs inside of folders in the classpath.
Yes, classpath should indeed be set to the current folder by default. However, setting it to a given folder is not the same as setting it to a specific JAR file.
Current practice here is to include individual files (other than JAR files, such as configuration files or data files) in the classpath. Is this considered correct and why or why not?
Update:
From http://docs.oracle.com/javase/7/docs/tooldocs/windows/java.html:
-cp classpath
Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by semicolons
(;). Specifying -classpath or -cp overrides any setting of the
CLASSPATH environment variable.
This question can also be interpreted as asking, does the above enumeration of directories, JAR archives, and ZIP archives contain an implicit "and nothing else"?
Update:
Un-accepting an answer because after I changed my classpath to include a directory rather than specific files, the program began working. So I am investigating whether there was some other issue involved or whether specifying individual files doesn't work (or at least, doesn't always work).
Update:
From http://docs.oracle.com/javase/7/docs/tooldocs/windows/classpath.html:
Classpath entries that are neither directories nor archives (.zip or .jar files) are ignored.
The classpath contains either directories or jar files
If your program loads those other files using Classloader.getResourceAsStream() (or some similar way) then it makes perfect sense to include those files in the classpath.
This is common e.g. for license files that are bundled with an application.
So yes this is considered correct - at least from my point of view.
From http://docs.oracle.com/javase/7/docs/tooldocs/windows/java.html:
-cp classpath
Specify a list of directories, JAR archives, and ZIP archives to search for class files.
Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists
of the current directory (.).
For more information on class paths, see Setting the Class Path
(http://docs.oracle.com/javase/7/docs/tooldocs/windows/classpath.html).
From http://docs.oracle.com/javase/7/docs/tooldocs/windows/classpath.html:
Classpath entries that are neither directories nor archives (.zip or
.jar files) are ignored.
Further:
Each classpath should
end with a filename or directory depending on what you are setting the
class path to:
For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the
full package name).
I have a program attempting to use classes from the jakarta-poi-3.0.2.jar in my /usr/share/java directory:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
etc...
When I compile, I get a package org.apache.poi.hssf.usermodel does not exist error for each of the imports above.
I have /usr/share/java on my classpath. Am I missing anything else?
/usr/share/java on the classpath does not bring in all jars in /usr/share/java.
Try putting /usr/share/java/jakarta-poi-3.0.2.jar in your classpath instead.
First up, you might want to upgrade - Apache POI 3.0.2 is over 3 years old, and there have been a lot of fixes since then!
As for your issue, you either need to list each jar file individually on your classpath, or you need to place all your jars into the jre lib directory (the contents is which is automatically included). The latter isn't generally recommended though...
You can't just list a directory on the classpath, and have the jars from within it picked up, sorry. Only individual class files will be loaded from a classpath directory, jars won't be
Bootstrap classpath is $JAVA_HOME/lib
but for user applications use user classpaths setting -classpath parameter like that:
java -classpath /usr/share/java/myclasses.jar
The following solution helped me
The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.
C:> sdkTool -classpath classpath1;classpath2...
-or-
C:> set CLASSPATH=classpath1;classpath2...
where:
sdkTool
A command-line tool, such as java, javac, javadoc, or apt. For a listing, see JDK Tools.
classpath1;classpath2 Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:
For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).
Multiple path entries are separated by semi-colons. With the set command, it's important to omit spaces from around the equals sign (=).
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
Classpath entries that are neither directories nor archives (.zip or .jar files) nor * are ignored.
Reference :http://download.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html