java CLASSPATH not working on command-line - java

System details:
Ubuntu 17.10
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.17.10.2-b12)
I can't get my java program to run. I don't know why it won't find the class. It compiles with the -classpath flag, but doesn't find the class when running.
$ ls -ltra
total 668
-rw-rw-r-- 1 bvpx bvpx 653275 Jan 19 14:45 javax.mail.jar
drwxr-xr-x 3 bvpx bvpx 4096 Jan 19 14:59 ..
-rw-r--r-- 1 bvpx bvpx 960 Jan 19 15:07 Example.java
drwxr-xr-x 2 bvpx bvpx 4096 Jan 19 15:07 .
Compiling without -classpath does not work (I thought -classpath defaulted to .?)
$ javac Example.java
Example.java:2: error: package javax.mail does not exist
Specifying the -classpath helps, the program now compiles and produces Example.class:
$ javac -classpath javax.mail.jar Example.java
$
Here's the source code:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Example {
static final int PORT = 587;
/* ... */
public static void main(String[] args) throws Exception {
/* ... */
Transport transport = session.getTransport();
try
{
System.out.println("Sending...");
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
}
catch (Exception ex) {
System.out.println("Error message: " + ex.getMessage());
}
}
}
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)
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)
Why can't java find the Example class?

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.

I had to set -classpath to include the current directory. According to the documentation classpath is delimited by :. The correct classpath string was:
javax.mail.jar:.
Below is a working example.
$ javac -classpath javax.mail.jar:. Example.java
$ java -classpath javax.mail.jar:. Example
Sending...
Email sent!
Another thing to note was that there was originally a package definition at the top of Example.java. I had to remove it.

Related

Unable to compile using Picocli

I'm a dev student
I would love to use Picocli in my project, unfortunately I doesn't understand how to compile using Picocli
I trie to follow the instruction given here https://picocli.info/ or here https://picocli.info/quick-guide.html but the step to compile aren't detailed. I'm not using Gradle nor Maven but they aren't really listed as required.
This is how it tried to compile the Checksum example given in the picocli.info webpage :
jar cf checksum.jar Checksum.java ; jar cf picocli-4.6.1.jar CommandLine.java && echo "hello" > hello
Then I simply copy paste this gived command : https://picocli.info/#_running_the_application
java -cp "picocli-4.6.1.jar:checksum.jar" CheckSum --algorithm SHA-1 hello
And get the following result :
Error: Could not find or load main class CheckSum
Caused by: java.lang.ClassNotFoundException: CheckSum
I tried to compile everything myself and then add the .jar like this :
java CheckSum -jar picocli-4.6.1.jar
But then the error output looks like this:
Exception in thread "main" java.lang.NoClassDefFoundError: picocli/CommandLine
at CheckSum.main(Checksum.java:33)
Caused by: java.lang.ClassNotFoundException: picocli.CommandLine
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
Witch I don't understand since I added the dependency.
What am I missing ?
Thanks in advance
The problem is that the command jar cf checksum.jar Checksum.java only creates a jar file (jar files are very similar to zip files) that contains the Checksum.java source file.
What you want to do instead is compile the source code first. After that, we can put the resulting Checksum.class file (note the .class extension instead of the .java extension) in the checksum.jar. The Java SDK includes the javac tool that can be used to compile the source code. Detailed steps follow below.
First, open a terminal window and navigate to a directory that contains both the Checksum.java source file and the picocli-4.6.1.jar library.
Now, the command to compile (on Windows) is:
javac -cp .;picocli-4.6.1.jar Checksum.java
Linux uses : as path separator instead of ;, so on Linux, the command to compile is:
javac -cp .:picocli-4.6.1.jar Checksum.java
The -cp option allows you to specify the classpath, which should contain the directories and jar/zip files containing any other class files that your project uses/depends on. Since Checksum.java uses the picocli classes, we put the picocli jar in the classpath. Also add the current directory . to the classpath when the current directory contains any classes. I just add . habitually now.
Now, if you list the files in the current directory, you should see that a file Checksum.class has been created in this directory.
Our Checksum class has a main method, so we can now run the program with the java tool:
On Windows:
java -cp .;picocli-4.6.1.jar Checksum
On Linux:
java -cp .:picocli-4.6.1.jar Checksum
Note that when running the program with java you specify the class name Checksum, not the file name Checksum.class.
You can pass arguments to the Checksum program by passing them on the command line immediately following the class name:
java -cp .:picocli-4.6.1.jar Checksum --algorithm=SHA-1 /path/to/hello
When your project grows, you may want to keep the source code and the compiled class files in separate directories. The javac compile utility has a -d option where you can specify the destination for the compiled class files. For example:
javac -cp picocli-4.6.1.jar:otherlib.jar -d /destination/path /path/to/source/*.java
This should generate .class files for the specified source files in the specified destination directory (/destination/path in the example above).
When you have many class files, you may want to bundle them in a single jar file. You can use the jar command for that. I often use the options -v (verbose) -c (create) -f (jar file name) when creating a jar for the compiled class files. For example:
jar -cvf MyJar.jar /destination/path/*.class /destination/path2/*.class
Enjoy!

java error: Exception in thread "main" java.lang.NoClassDefFoundError

I am a beginner in java and taking the course Algorithm, which is provided by Princeton. In the course, professor asked us to download algs4.jar to a folder and add algs4.jar to the classpath.[1]
I followed it step by step, and try to program a HelloWorld like
import edu.princeton.cs.algs4.StdOut;
public class HelloWorld {
public static void main(String args[]) {
StdOut.print("Hello World!");
}
}
However when I compile the file, console reminds me that
NPP_EXEC: "java_Compile_Run"
NPP_SAVE: G:\java\helloworld\HelloWorld.java
javac -encoding UTF-8 "G:\java\helloworld\HelloWorld.java"
Process started >>>
<<< Process finished. (Exit code 0)
==========编译成功后开始运行==========
java -cp "G:\java\helloworld" "HelloWorld"
Process started >>>
Exception in thread "main" java.lang.NoClassDefFoundError:
edu/princeton/cs/algs4/StdOut
at HelloWorld.main(HelloWorld.java:5)
Caused by: java.lang.ClassNotFoundException: edu.princeton.cs.algs4.StdOut
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
<<< Process finished. (Exit code 1)
================ READY ================
I have checked my classpath, and my programming file. What does this error mean? And how can I fix it?
Any advice is helpful. Thank you.
[1] http://algs4.cs.princeton.edu/code/
If you're referring to a jar file that should be on the classpath, you must name it explicitly. E.g.
java -cp "G:/java/helloworld;G:/whereever/algs4j.jar" HelloWorld
Do they really provide another name for System.out? In this case you can also safely ignore that jar by using System.out instead of StdOut
According to the instructions of this course you should use javac-algs4 to compile and java-algs4 to execute if you imported their library.
If you want to know the differences between these two commands and original commands javac and java, you could
use type command to find where is this command is
use cat or vim to see what's the content of this command
You can see that java-algs4 added -cp parameter to original java command just as Olaf Kock said.
You'll have to probably use -cp flag to set the class path to include the package.
While using the -cp flag, don't forget to include the current working directory using .
So, something like javac -cp thejar.jar:. should work in linux or javac -cp thejar.jar;. should work for windows
For Mac, using M1, cd in your folder:
javac -classpath ".:./algs4.jar" HelloWorld.java
java -classpath ".:./algs4.jar" HelloWorld
I just included a classpath argument in both javac and java commands like so:
javac -classpath ".;drive\path\to\algs4.jar" Hello.java
and
java -classpath ".;drive\path\to\algs4.jar" Hello
Also if you are manually adding the CLASSPATH environment variable, then remember to close and restart the cmd console.
You imported class StdOut in your java code (import edu.princeton.cs.algs4.StdOut;), you have to tell java how stdOut implement
Accroding to the link you provided (http://algs4.cs.princeton.edu/code/). You have to follow the "Installing the textbook libraries." section to install this lib first.

How to use the apache commons java library on Ubuntu?

I am a Java beginner and trying to figure out how to use the apache commons lib.
Here is a source file Randstr.java:
import org.apache.commons.lang3.RandomStringUtils;
class Randstr {
public static void main(String[] args) {
String s = RandomStringUtils.random(12);
System.out.println(s);
}
}
I have the commons-lang3-3.1.jar file in /usr/share/java/ and have created a symlink in the current dir. Then I compiled it like this: javac -cp commons-lang3-3.1.jar Randstr.java, the complilation was fine, but when I execute java Randstr, I got the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils
at Randstr.main(Randstr.java:5)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
And if I don't specify the jar file in the classpath, it will not even compile:
javac -cp . Randstr.java
# Randstr.java:1: error: package org.apache.commons.lang3 does not exist
# import org.apache.commons.lang3.RandomStringUtils;
# ^
# Randstr.java:5: error: cannot find symbol
# String s = RandomStringUtils.random(12);
# ^
# symbol: variable RandomStringUtils
# location: class Randstr
# 2 errors
javac -cp /usr/share/java/ Randstr.java
# Randstr.java:1: error: package org.apache.commons.lang3 does not exist
# import org.apache.commons.lang3.RandomStringUtils;
# ^
# Randstr.java:5: error: cannot find symbol
# String s = RandomStringUtils.random(12);
# ^
# symbol: variable RandomStringUtils
# location: class Randstr
# 2 errors
From reading other questions on stackoverflow, I see this can be solved by using an IDE, but I prefer a simple editor at the moment.
If you can compile it with
javac -cp commons-lang3-3.1.jar Randstr.java
then you can run it with
java -cp commons-lang3-3.1.jar:. Randstr
The JAR file has to be in the classpath.
Edit your profile file. vim ~/.bashrc
In your profile file add the following line:
export CLASSPATH=/usr/share/java/commons-lang3-3.1.jar:.
Log out and back in. Or source your profile file in the windows you have open. You can always add your classpath to every java and javac command you invoke but that becomes a pain. With the CLASSPATH environmental variable you don't have to add it on the command line any more. Note that if you are using an IDE such as NetBeans or Eclipse you still might have to add the library to your project's libraries within the IDE.
Clearly the contents of /usr/share/java/ don't automatically get added to the classpath - it's just a common location where APT packages put Java libraries. It's up the developer to reference them correctly.
JARs in the ext/ subdirectory of a Java installation do get added to the classpath automatically. However, do not put your own JARs in there. It's a terrible practice because it doesn't match how Java apps are deployed "in the real world".
The correct way is using the -cp parameter explicitly when compiling AND running your app. Java doesn't compile library code into your .class files, a .class file only refers to names of other classes which are then loaded as-needed from the class path when your app runs. The -cp parameter takes only .jar files, or directories with .class files in them. You can also use wildcards in the value of that parameter. For more information on wrangling the class path, check the tool documentation on setting the class path.
You using a build tool that sets it for you automatically, like an IDE or Maven or another build system with dependency management. (Gradle or Ant+Ivy.) If you're writing a Java app that uses third party libraries, I very strongly suggest you learn and use one of those. (Also, most IDEs can work with Maven's configuration files letting you use the same build settings in a team with people using mixed or no IDEs.) Generally if you're invoking a compiler directly you're not doing it right.

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

I've been working on this for about an hour and thumbing through Q&As on stackoverflow but I haven't found a proposed solution to my problem. I'm sorry if this is a duplicate, but I couldn't find any duplicate question with an answer that solved my specific problem.
I am trying to write and compile a java program from terminal for the first time (up until this point I have been using Eclipse for java and VIM for everything else, but I feel its time to switch entirely to VIM). Here is my current HelloWorld code:
package main;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
I compile and run using the following commands (specifying the classpath to ensure that isn't the problem):
javac -cp "./" HelloWorld.java
java -cp "./" HelloWorld
This gives me the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: main/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
I know it is seeing the file HelloWorld.class and trying to access the class HelloWorld because if I change the run command to:
java -cp "./" Foo
I get an entirely different error message:
Error: Could not find or load main class Foo
I have tried several dozen pages worth of troubleshooting and come up short, including the following:
Exception in thread "main" java.lang.NoSuchMethodError: main
http://introcs.cs.princeton.edu/java/15inout/mac-cmd.html
java -version yields:
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode)
My operating system is LinuxMint and uname -a yields:
Linux will-Latitude-D620 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
package main;
This means that your class resides in the main package, and its canonical name is main.HelloWorld.
Java requires that package names should also be mirrored in the directory structure. This means that:
Your HelloWorld.java file should be in a directory named main
You should execute javac and java from the directory containing main, not from main itself
The classpath should contain the directory where the main directory is, not main itself
java expects the canonical name of the class to execute, so main.HelloWorld
So, to recap:
You should have something like myproject/main/HelloWorld.java
From myproject, run javac main/HelloWorld.java
From myproject, run java -cp ./ main.HelloWorld
You've put your class in a package named "main", but you're trying to treat it like it isn't in a package. Since you put package main; at the top of your source file, you need to put HelloWorld.java in ./main, then run javac ./main/HelloWorld.java, followed by java -cp . main.HelloWorld.
These commands will get you the working example you're trying to build:
mkdir main
echo 'package main; public class HelloWorld { public static void main(String... args) { System.out.println("Hello World"); } }' > main/HelloWorld.java
javac main/HelloWorld.java
java -cp . main.HelloWorld
As a beginner you might encounter a very similar scenario where the error output is the same. You try to compile and run your simple program(without having any package set) and you do this:
javac HelloWorld.java
java HelloWorld.class
This will give you the same java.lang.NoClassDefFoundError since java thinks HelloWorld is your package and class your class name. To solve it just use
javac HelloWorld.java
java HelloWorld
See the Java page - Lesson: Common Problems (and Their Solutions)
Problem:
Basically, the Exception in thread "main" java.lang.NoClassDefFoundError:
means, that the class which you are trying to run was not found in the classpath.
Solution: you need to add the class or .jar file which contains this class into the java classpath. When you are running a java class from the command line, you need to add the dot (.)
java YourSingleClass -cp .
into the classpath which tells the JVM to search for classes in actual directory.
If you are running a class from a .jar file, you need to add this jar file into the classpath:
java org.somepackage.SomeClass -cp myJarWithSomeClass.jar

java -classpath ./sqljdbc4.jar myclassname

if I run java -classpath ./sqljdbc4.jar myclassname error is
Exception in thread "main" java.lang.NoClassDefFoundError:myclassname
if I run java myclassname error is
java.lang.ClassNotFoundException:
com.microsoft.sqlserver.jdbc.SQLServerDriver
It is on Linux. How to fix it?
The directory or jar containing your classes package tree (i.e. the directory containing the com directory, in the following example) must be in the classpath. And the sqljdbc4.jar must also be. Put both in the classpath:
java -cp ../classes:./sqljdbc4.jar com.foo.bar.MyClassName
Also note that you need to use the fully qualified name of the main class (as the above example shows), and that class names in Java use CamelCase by convention.

Categories