spring-boot-maven-plugin created jar run by using classpath - java

used spring-boot-maven-plugin to build my jar, i'm to run it via
java -jar myExample-1.0-SNAPSHOT.jar
but it throw error
Error: Could not find or load main class com.manish.myexample.Example
while running via
java -cp myExample-1.0-SNAPSHOT.jar com.manish.myexample.Example
is because of spring-boot-maven-plugin ? and even tried
java -cp libs/myExample-1.0-SNAPSHOT.jar: BOOT-INF.classes.com.manish.myexample.Example

You must ensure that you add the location of your .class file to your classpath. That is usually first mistake.
...and some general reasons why Java cannot find the class:
you made a mistake with the classname argument;
the application's classpath is incorrectly specified: the wrong directory is on the classpath, the subdirectory path doesn't match or dependencies missing from the classpath;
the class has been declared in the wrong package.

Related

javac(compile) and java(run) package work at different directory structure levels?

I just noticed that for compiling I need to specify the exact path from my current location for the java source files that needs to be compiled, but to run the class file, all I need is the class qualified name.
Does the java command recursively look inside all the folders in my current directory to find the class and execute it?
if so why doesn't the javac work similarly but expects to have the absolute path?
From my project folder location,
javac -cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:target/classes" src/main/java/app/Assignment04.java
java -cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:target/classes" app.Assignment04
EDIT:
when I explicitly mentioned path like this;
java -cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:src/main/java/com/scg/util/*:src/main/java/com/scg/domain/*:src/main/java/app/*" app.Assignment04
I get
Error: Could not find or load main class app.Assignment04
although I give src/main/java/app/ in my classpath
-cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:target/classes"
is the important bit. When compiling the classpath argument is not considered for the source files themselves (only to resolve dependencies).
When running with java the named class is sought on the classpath, which includes the target directory explicitly as target/classes. Thus that location is also used to find the class in question.

Class in jar not found at runtime, but was used to compile

After I build this project from an ant file, I recieve a jar that contains all of the classes I built. When I try to run this jar, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j3d/SceneGraphObject
This error indicates that a one of the jars, specifically the j3dcore.jar from java3d, I am using can not be found. However, this jar is on the classpath when compiling through the ant build into the class files.
Why can this class not be found at runtime, but it is found at compile time? Do I have to manually change my classpath in my shell when running the jar as well as change it in the ant build?
If I add the jars to my classpath using java -cp j3d/*.jar -jar idv.jar
I get the error Error: Could not find or load main class j3d.j3dutils.jar
Do I have to manually change my classpath in my shell when running the jar as well as change it in the ant build?
Yes, absolutely. Making a class available at compile-time doesn't embed the class into your output or anything like that. It just makes it available to the compiler (to find out what methods are present etc).
If I add the jars to my classpath using java -cp j3d/*.jar -jar idv.jar
Yes, it would - because that's being expanded into:
java -cp j3d/foo.jar j3d/bar.jar ... -jar idv.jar
It's not clear to me whether -cp is meant to work at all with -jar, given this documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
One option is to set the classpath within the manifest of the jar file itself. For example:
Class-Path: j3d/foo.jar j3d/bar.jar
Another would be to ignore the -jar command-line option for now, and use:
java -cp j3d/*:idv.jar your.class.name.Here
Note the * rather than *.jar, as documented:
As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program cannot tell the difference between the two invocations).

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

Under Linux I am trying to run a jar file as follows:
java -jar plantuml.jar -testdot
while having CLASSPATH set to any of the following (the file is located at /home/user/plantuml.jar):
export CLASSPATH=/home/user
export CLASSPATH=/home/user/
export CLASSPATH=/home/user/plantuml.jar
In either case, no matter how I define CLASSPATH, the java command gives an error Unable to access jarfile plantuml.jar. What am I doing wrong here?
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.
export CLASSPATH="/path/to/class_or_jar1":"/path/to/class_or_jar2":"${CLASSPATH}"
Maybe you are missing name of the main class or path to the jar. Have you tried execute it:
java -jar full_path/plantuml.jar package.YourClass -testdot
Is your program depending on other classes? If yes you might want to add -cp parameter.
The classpath is used to find classes when you refer to them by name. It's essentially a list of paths (directories AND jar/zip files) where the JVM needs to look for classes, or other resources when using methods like ClassLoader.getResourceAsStream().
The value passed to the -jar option on the command line is the file-path to the JAR file.
So, it won't find a jar file if you are only referring to the jar file by name. The JAR file path in the CLASSPATH is supposed to be a path element that 'contains' other resources.
What you need to do here, is either
Provide the full path to the jar file when trying to execute the jar
Set the classpath to the jar file's path, and run the java command giving the name of the main class you want to execute.

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;

Does java -jar option alter classpath options

I have a jar file which mentions the main class in the manifest.
When I try to execute the jar using the following command
java -cp .;./* com.foo.MainClass
The code executes and works.
When I try to execute the jar using the following command
java -cp .;./* -jar myjar.jar
I get class not found execptions for some jars which are in the same folder as myjar.jar. I hoping that the -cp option will include those jars in class path.
I modified my code to print java.class.path property. In the first case it listed all jars in the current directory, in second case it just listed myjar.jar
I also modified the manifest to add Class-Path element to it with all jars. Then the second command works. But in my code I am trying to load a aribtrary class whose name is provided at command prompt, so I want the class path to contain all jars in a folder. How do I make the second command work in this scenario?
From this,
An executable JAR must reference all the other dependent JARs it
requires through the Class-Path header of the manifest file. The
environment variable CLASSPATH and any class path specified on the
command line is ignored by the JVM if the -jar option is used.
You will need your own classloader to deal with this. -jar only respects the information in the Manifest and wildcards are not allowed there.
You might find the example of a reloadable class useful: http://www.exampledepot.com/egs/java.lang/ReloadClass.html
Here is a good discussion on this issue.

Categories