javac: package org.apache.poi.hssf.usermodel does not exist - java

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

Related

How to set the CLASSPATH to import jar package in a short format

Can this way import multiple jars?
set CLASSPATH=C:\dependency\*;C:\location\*
Is this means all jars below the dependency is imported?
Wildcards are allowed in the latest Java 6. See the document for details:
http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
Let's take a look at this example
java -classpath "./libs/*" Test
The * here refers to all jar files in the LIBS directory. You can't write java -classpath "./libs/*. jar" test like this
If there are both jar files and class files in the LIBS directory, we all want to reference them, so we need to write
java -classpath "./libs/*;./libs/" Test
Note: in Windows system, the separator is; in UNIX system, the separator is:
Note that LIBS/* does not contain jar files in subdirectories under LIBS directory, such as LIBS/folder1/a.jar
If you want to include subdirectories, you need to be clear about them, such as
java -cp "./libs/*;./libs/folder1/*" Test

Running Java command including all JARs in current folder

I have just shifted back from an IDE to Notepad to write a Java program. The program is using 20 JARs. I compiled successfully. When I decided to run the Java class file using
java -cp ".\\*" MyProgram
it was giving the standard error "Couldn't find or load main class....".
I was confused because when I used to run the java command with all files in an existing folder, it would just get those JARs as the current folder is already in the classpath. As the program is running from the current folder, I tried using -cp "." to include it explicitly in the classpath but that didn't work either.
Finally I was able to run the program with this command:
java -cp ".\\*;." MyProgram.java
I am asking this question to understand the actual logic behind Java's classpath.
Correct me if I am wrong, but I think that the JAR is just a standard archive in which all the packages are encapsulated in respective folders. If all the JARs are in my current folder including my main class file then why can't I run it with:
java -cp "." MyProgram
or simply:
java MyProgram
If the problem is with the multiple JAR files to include and that's why we used ".\\*" to include all the JARs in the classpath, then why do we have to explicitly include the current folder again in the classpath using:
java ".\\*;." MyProgram
To include all jar required to run your program in command prompt use wildcard *:
java -classpath E:\lib\* HelloWorld
You are using "." that defines current directory and "./*" defines all files in current directory.
The class path is a list of jar files and directories containing the classes and resources of your program. Mentioning a jar file adds its contents to the class path.
"./*" will get you only the jar files in the current directory, "." adds the current directory to the class path. This allows to access all classes (and the jar files as raw file resources but not its contents, i.e. the classes contained in them).
If you need both in the class path, you have to specify both.
You've answered your own question, sort of.
. means that it will look for .class files in the current directory.
JARs act just like a directory. So to have the abc.jar "directory" you would specify abc.jar in your classpath.
If you need both the .class files present in the current directory, and the .class files packaged into JARs found in the current directory, you would have the following classpath: -cp ".:*.jar
All the answers here are telling you to use the wildcard without extension (* or ./*) but this is a bad practice, you don't want Java to go look into irrelevant files, so specify the extension: *.jar.
"." means current directory not files in the directory
"./*" means all files in current directory.
So you want to use all jars in current directory so 2nd will work

Java NoClassDefFoundError even when Jars in same folder

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;

Setting classpath in windows

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+).

About import statement in Java

test.java:
import javax.media;
Which directories are javax.media searched from?
I suppose those in CLASSPATH specified by javac -cp will of course be searched.
But will it also search in the directory where test.java locates?
And are there any other possible places that will be searched?
Consult the specifications for your compiler. For sun's javac, the search order is described in its manual.
Depending on a few things the manual details, class files in the paths set in the CLASSPATH environment variable, class files in the paths set the -classpath command line option, source files in the paths from the '-sourcepathoption, source files in user classpath (if-sourcepathisn't given), and either the JVM's default boot and extension paths or the paths given by the-bootclasspathand-extdirs` options.
As javax.media is a JVM extension, javac would look either in the JVM's extensions directory, or that provided by -extdirs, then in user class folders, then in user source folders.
The simplified version is: It's only directories and jars in your classpath, but quite often people put "." in their classpath which would allow searching of directories under the current in the same way as any other classpath directory.

Categories