Java Classpath - Linux

Set classpath permanently in Linux

You could add that export statement to the end of your bash init file ~/.bashrc, therefore it will in effect all the time.

By the way, a better setting of CLASSPATH would be

export CLASSPATH="/path/to/file":"/path/to/file2":"${CLASSPATH}"

this will also preserve the initial value of that environment variable.

Java classpath - Linux

I think you should not put any paths that a local to your home directory in a system wide file. I would leave /etc/environment well alone, unless you provide some changes, that are necessary or beneficial to all users.
Put any changes to the CLASSPATH in your .bashrc in your home directory.

  CLASSPATH=$CLASSPATH:/home/foo:/home/foo/Java_code/my_code
export CLASSPATH

This way you can source it and any newly started bash will have the settings right at once.

How to add multiple jar files in classpath in linux

You use the -classpath argument. You can use either a relative or absolute path. What that means is you can use a path relative to your current directory, OR you can use an absolute path that starts at the root /.

Example:

bash$ java -classpath path/to/jar/file MyMainClass

In this example the main function is located in MyMainClass and would be included somewhere in the jar file.

For compiling you need to use javac

Example:

bash$ javac -classpath path/to/jar/file MyMainClass.java

You can also specify the classpath via the environment variable, follow this example:

bash$ export CLASSPATH="path/to/jar/file:path/tojar/file2"
bash$ javac MyMainClass.java

For any normally complex java project you should look for the ant script named build.xml

How to set CLASSPATH in Linux to let java find jar file?

You have to supply the complete path after the parameter -jar. So for your example you have to call

java -jar /home/user/plantuml.jar -testdot

The $CLASSPATH is only evaluated to find additional files (classes/resources) but not the jar file defined in the command line.

Linux equivalent of including the classpath during compilation

On Linux, you have to use : (colon) in place of ; (semicolon) as the path separator in Java options.

Also, if you have a classpath variable, in most common Linux shells it is referenced by $classpath rather than by %classpath%

javac -classpath ../../lib/OneWireAPI.jar:$classpath -d . ./src/ReadTemp.java

java CLASSPATH not working on command-line

You seem to be missing some fundamental concepts here.

The classpath gives a list of directories and JAR files to search for needed classes. When trying to load a class foo.bar.MyClass that is not part of the standard library, the default classloader will look for it in each classpath element in turn, in order, until it finds the class or runs out of elements.

Note well, however, that it searches by fully-qualified name. For classpath entries that are directories, that means that it looks for foo/bar/MyClass.class relative to the directory. For classpath entries that are JAR files, it looks for foo/bar/MyClass.class relative to the root of the JAR. Classes that belong to the unnamed default package are a little special, or so it may seem, because their class files (e.g. InDefaultPackage.class) are expected to be located directly in the root of the designated JAR or directly in the specified directory.

Compiling without -classpath does not work (I thought -classpath
defaulted to .?)

$ javac Example.java 
Example.java:2: error: package javax.mail does not exist

The classpath does default to .. This is the name of a directory, so when searching it for classes in, say, the javax.mail package, it looks for a subdirectory javax/mail, and if that is found, it examines the class files within. Note that it does not descend into JAR files it discovers in the directory tree. It looks only in those JARs explicitly named in the classpath.

The error message is telling you that javac didn't find any classes at all from the javax.mail package. You could have solved it either by specifying the JAR in the compilation classpath (as ultimately you did) or by unpacking the JAR in the current directory.

Specifying the -classpath helps, the program now compiles and produces
Example.class:

$ javac -classpath javax.mail.jar Example.java
$

Note that the compiler will store the classfile in a directory structure corresponding to its package, just where the java command will look for it.

Running the program produces this error:

$ java -Xdiag -classpath javax.mail.jar Example 
Error: Could not find or load main class Example
java.lang.ClassNotFoundException: Example
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

You clarified in your answer that you solved this problem by removing a package statement from Example.java. That's ok, but it doesn't really explain the problem, which is that java expects you to give it the fully-qualified name of the class. That includes the package name if the class is in a named package. Thus, if Example.java contained this package statement:

package com.my;

then the class name you would need to specify to java would be com.my.Example. You specified just Example, which designates a class named "Example" in the default package, and your solution to the class not found problem was to move your class into the default package.

Note also that it is conventional and helpful to lay out your Java source files, too, in a directory structure matching their package structure. Thus, the source file for class com.my.Example would conventionally be located in com/my/Example.java. The Java compiler will rely on this scheme to locate sources for classes that it does not find.

Running java without -classpath causes the JNI to not find
javax/mail even though it's in the directory.

$ java -Xdiag Example 
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
at java.lang.Class.getDeclaredMethods0(Native Method)

No, javax/mail/Address was not in the directory. It was in a JAR file in the directory. That's not at all the same thing, and the difference is significant.

Classpath does not work under linux

The classpath syntax is OS-dependent. From Wikipedia :

Being closely associated with the file
system, the command-line Classpath
syntax depends on the operating
system. For example:

on all Unix-like operating systems
(such as Linux and Mac OS X), the
directory structure has a Unix syntax,
with separate file paths separated by
a colon (":").

on Windows, the directory structure
has a Windows syntax, and each file
path must be separated by a semicolon
(";").

This does not apply when the Classpath
is defined in manifest files, where
each file path must be separated by a
space (" "), regardless of the
operating system.



Related Topics



Leave a reply



Submit