Specify Java classpath - java

I am on a Linux and have a WAR directory with WEB-INF/lib and WEB-INF/classes directories. I further have a class with a main method.
I want to make java execute the main method and have all libraries on the claspath, so I call
java -cp /path/to/WAR/WEB-INF/lib/*:/path/to/WAR/WEB-INF/classes/* this.is.my.package.Main
I get a
Error: Could not find or load main class this.is.my.package.Main
However, when I call from inside WEB-INF/classes directory
java this.is.my.package.Main
I get the Exception because a library class is missing
Exception in thread "main" java.lang.NoClassDefFoundError: ...
What am I doing wrong?

The classes directory itself must be in the classpath. Not all its files:
java -cp /path/to/WAR/WEB-INF/lib/*:/path/to/WAR/WEB-INF/classes

Related

I am having trouble with running a jar file

I try to run a java program with the following script:
#!/usr/bin/env bash
java -cp dl.jar Elevator
Elevator is the main class. But I get an error every time.
Error: Could not find or load main class Elevator
Caused by: java.lang.ClassNotFoundException: Elevator
My classes are all in the .jar file under the directory src/hw4. I have tried adding src/hw4 to the classpath, but to no avail.
You should not use direct class name. You have to use the full package.
If in your IDE you wrote for exammple: package hw4; and you want to run the class Elevator, use this :
java -cp dl.jar hw4.Elevator
It seems to correspond to your jar file.
The src folder is not on the class path. It's just a directory in your IDE to order all files as project files and project content.

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

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.

Executing a class in jar file

I have created an application that uses selenium-server-standalone-2.47.1.jar and javax.mail.jar. The code works on eclipse, but I would like to run the same from command line. So I exported the project to a runnable jar file which contains both selenium and javax.mail.jar.
My code contains RTC.java which has the Main function and another Ex.java.
Both the class files are generated in com folder.
My App1.jar file is located in C:\installers.
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError: org.openqa.selenium.WebDriver
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
What am I doing wrong?
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError:
org.openqa.selenium.WebDriver
That exception usually means that a .class file was found but it didn't contain the right class. Check how you're putting it into the JAR. It's directory and file name must match its package and class name.
It sometimes also seems to mean that a secondary class wasn't found. Normally secondary JAR files are mentioned in the class-path entry of the manifest of the main JAR file, along with the main-classname, so you can use
java -jar App1.jar
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
I'm not surprised. If the first command got as far as it did, the JAR filename doesn't start with a dot. Cannot imagine why you tried this. It is nonsense.
I found the solution. I need not create a jar file which has the reference jars. I have to mention my jar which has my code and the reference jars that I am using in the class path.
My code is in App.jar.The reference jars are
selenium-server-standalone-2.47.1.jar and
javax.mail.jar.
so I used
c:\installers> java -cp selenium-server-standalone-2.47.1.jar;javax.mail.jar;App.jar com.RTC
Thank You.

Failing to execute the jar file using java -jar command

I have created a jar file usign maven2 build. I am trying to run that jar file using the command:
java -jar sample.jar com.app.Test
Test being the class which is having the main method. But i am getting this exception:
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Can any one help me to solve this exception and run the jar file?
Thanks in advance.
If you want to run the Test class, you should use
java -cp sample.jar com.app.Test
This way, you add the jar to the classpath and then run the specified main class.
What java -jar does is that it executes a runnable jar file (which defines its own main class in the manifest file). Any parameters after that would not be used to specify the class, but end up in the String array passed to the main method.
So if you have a properly constructed runnable jar file, it should just be
java -jar sample.jar
If you are using Maven, you may need to use maven's install command. You can find the format here
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Looks like you do need a manifest and a Main-Class attribute, if you don't have one.
If you are using java 7 a bug is filed here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922

Setting Java Classpath to Load a Class File

I'm new to Java, and I'm unsure how to access a class file located in a specific directory from a separate program jar.
For example, I have an third party jar file located in /, which is supposed to load MyClass located in /mylib/MyClass.class, so I tried running:
java -jar mainprog.jar -classpath "/mylib" MyClass
but I'm getting the error:
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass
Caused by: java.lang.ClassNotFoundException: MyClass
at java.net.URLClassLoader$1.run(URLClassLoader.java:221)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:209)
at java.lang.ClassLoader.loadClass(ClassLoader.java:324)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:269)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:337)
What am I doing wrong?
When you use "-jar" then only the Class-Path attribute defined in the META-INF/MANIFEST.MF file inside the jar file will influence the classpath.
It will also ignore the MyClass argument (or more specifically: interpret it as an argument to the main-class defined in the MANIFEST.MF).
If you simply want to call a class from that jar call it like this:
java -cp mainprog.jar:/mylib MyClass
// or using this one on windows:
java -cp mainprog.jar;/mylib MyClass
In your command line you are trying to run MyClass as a program, which based on your description is not what you want.
You need to figure out what the main class is that is used to execute the program in the jar. You could unpack the jar file with jar -xf mainprog.jar and look at the META-INF/MANIFEST.MF file. It should have an entry that indicates that the main class of the jar is (I can't remember the name of the entry right now).
After than change your command line to something like this:
java -classpath /mainprog.jar:/mylib package.name.to.jar.MainClass

Categories