20

I wrote a program that works on my laptop perfectly, but I really want it to work on a server that I have. Using NetBeans, I've clean and built the project. I copied the contents of the folder dist on my server but I cannot seem to get to work by using command

java -jar nameOfFile.jar

I get the error

java.lang.NoClassDefFoundError: org/....

I have been doing some reading and from what I gather is that I need to pretty much specify where the libraries that I've used are located. Well they are located in a subfolder called lib.

Question:

So what would I need to do in order to be able to run my jar?

Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
user1198778
  • 379
  • 1
  • 4
  • 10

6 Answers6

12

CLASSPATH is an environment variable that helps us to educate the Java Virtual Machine from where it will start searching for .class files.

We should store the root of the package hierarchies in the CLASSPATH environment variables.

In case of adding or using jar libraries in our project, we should put the location of the jar file in the CLASSPATH environment variable.

Example: If we are using jdbc mysql jar file in our java project, We have to update the location of the mysql jar file in the CLASSPATH environment variable. if our mysql.jar is in c:\driver\mysql.jar then

We can set the classpath through DOS in Windows

set CLASSPATH=%CLASSPATH%;c:\driver\mysql.jar 

In Linux we can do

export CLASSPATH=$CLASSPATH:[path of the jar]

Hope it helps!

Bharti Rawat
  • 1,949
  • 21
  • 32
Chinmay Patel
  • 430
  • 6
  • 12
7

Try that:

java -classpath "$CLASSPATH:nameOfFile.jar:lib/*" path.to.your.MainClass

What this does is setting the classpath to the value of $CLASSPATH, plus nameOfFile.jar, plus all the .jar files in lib/.

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
3

Classpath

A compiler(e.g. javac) creates from .java - .class files and JVM uses these .class files.

classpath - local codebase[About] - points on the root of source. classpath + import_path = full path

For example for MacOS

//full path
/Users/Application.jar/my/package/MainClass

//classpath
/Users/Application.jar

//import_path
my.package.MainClass

Android classpath

ANDROID_HOME/platforms/android-<version>/android.jar
//e.g
/Users/alex/Library/Android/sdk/platforms/android-23/android.jar
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
1

When you use a META-INF/MANIFEST.MF file to specify the Main-Class dependencies must be specified in the manifest too.

The -jar switch ignores all other classpath information - see the tools docs for more.

McDowell
  • 107,573
  • 31
  • 204
  • 267
0

You need to set class path using

The below works in bash . This is temporary

set CLASSPATH=$CLASSPATH=[put the path here for lib]

If you want it permanent then you can add above lines in ~/.bashrc file

export CLASSPATH=$CLASSPATH:[put the path here for lib]:.
madan ram
  • 1,260
  • 2
  • 19
  • 26
0

You have 2 questions, one is the "title question" and another is the "foot note question" after elaborating your problem.

Read this documentation bellow to get a better understanding of CLASSPATH.

https://docs.oracle.com/javase/tutorial/essential/environment/index.html https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

This is fast and straight forward for what you need.

For your first question, this will do:

The documentation recommends us to set a classpath for every application we are running at the moment using (use in the command-line):

    java -classpath C:\yourDirectoryPath myApp

For your second question, look this exercise in the java documentation. It seems to be the same problem:

https://docs.oracle.com/javase/tutorial/essential/environment/QandE/answers.html

Answers to Questions and Exercises: The Platform Environment

Question 1.A programmer installs a new library contained in a .jar file. In order to access the library from his code, he sets the CLASSPATH environment variable to point to the new .jar file. Now he finds that he gets an error message when he tries to launch simple applications:

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

In this case, the Hello class is compiled into a .class file in the current directory — yet the java command can't seem to find it. What's going wrong?

Answer 1. A class is only found if it appears in the class path. By default, the class path consists of the current directory. If the CLASSPATH environment variable is set, and doesn't include the current directory, the launcher can no longer find classes in the current directory. The solution is to change the CLASSPATH variable to include the current directory. For example, if the CLASSPATH value is c:\java\newLibrary.jar (Windows) or /home/me/newLibrary.jar (UNIX or Linux) it needs to be changed to .;c:\java\newLibrary.jar or .:/home/me/newLibrary.jar."