What is java classpath linux

What exactly is a class path in java?

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.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?

6 Answers 6

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 
export CLASSPATH=$CLASSPATH:[path of the jar] 
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/ .

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

//full path /Users/Application.jar/my/package/MainClass //classpath /Users/Application.jar //import_path my.package.MainClass 
ANDROID_HOME/platforms/android-/android.jar //e.g /Users/alex/Library/Android/sdk/platforms/android-23/android.jar 

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.

You need to set class path using

Читайте также:  Draw on screen linux

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]:. 

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.

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:

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.»

Источник

Java Classpath

Learn how to set classpath in Java either as an environment variable and pass as the command-line argument. During runtime of any Java application, the CLASSPATH is a parameter that tells the JVM where to look for classes and packages.

  • The default value of the classpath is “ . ” (dot) , meaning that only the current directory is searched for dependencies.
  • Specifying either the CLASSPATH environment variable or the -cp command line switch overrides this value.
  • The order in which you specify multiple classpath entries is important. The Java interpreter will look for classes in the directories in the order they appear in the classpath variable.

Java Classpath separators are OS specific.

Windows – ; [Semicolon]
Linux/Unix – : [Colon]

Читайте также:  Служба днс в линукс

1. Setting CLASSPATH as Environment Variable

When you have set the location of jar files that are always required during the application runtime, then it’s probably best to add them in the machine’s environment variable ‘CLASSPATH’ .

During application runtime, application class loader will always scan the jar files and classes at specified paths in this variable.

To set CLASSPATH environment variable, find the location of user environment variables in your machine and add all paths where Jar files are stored. Use the separator between different two folders, jar files or classes.

You can find the user environment variables window by –

  1. From the desktop, right-click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. In the section System Variables, find the CLASSPATH environment variable and select it. Click Edit. If the CLASSPATH environment variable does not exist, click New .
  5. Add all folders separated with a separator. Click OK. Close all remaining windows by clicking OK.

System Properties

If you are creating CLASSPATH for the first time, you need to specify the name for the variable name in Windows 10. Use ‘.’ (dot) to denote the current directory.

2. Setting CLASSPATH from Command Line

Use -classpath argument to set classpath from command prompt/console. Use the following command to set the classpath for different requirements.

Let’s say we have a folder named dependency where JAR files and other classes are placed.

2.1. Add a Single Jar in CLASSPATH

Below syntax examples will add single jar file in classpath.

//WINDOWS $ set CLASSPATH=.;C:\dependency\framework.jar //Linux/Unix $ export CLASSPATH=.:/dependency/framework.jar

2.2. Add Multiple Jars in CLASSPATH

Читайте также:  Simply linux 32 bit

Below syntax examples will add more than one jar file in classpath. To do so, simply use the delimiter for your operating system (either ; or : ) as a separator between the locations specified for the CLASSPATH.

To add all JAR files present in a directory, use wildcard character ( ‘*’ ).

//WINDOWS $ set CLASSPATH=C:\dependency\framework.jar;C:\location\otherFramework.jar $ set CLASSPATH=C:\dependency\framework.jar;C:\location\*.jar //Linux/Unix $ export CLASSPATH=/dependency/framework.jar:/location/otherFramework.jar $ export CLASSPATH=/dependency/framework.jar:/location/*.jar

2.3. Add Multiple Classes in CLASSPATH

Many times, you may need to add individual classes in classpath as well. To do so, simply add the folder where classfile is present. e.g. let’s say there are five .class files are present in location folder which you want to include in classpath.

//WINDOWS $ set CLASSPATH=C:\dependency\*;C:\location //Linux/Unix $ export CLASSPATH=/dependency/*:/location

As a best practice, always organize all JAR files and application classes inside one root folder. This may be the workspace for the application.

Please note that subdirectories contained within the CLASSPATH would not be loaded. In order to load files that are contained within subdirectories, those directories and/or files must be explicitly listed in the CLASSPATH.

If your CLASSPATH environment variable was set to a value that is not correct, then you can unset CLASSPATH by specifying an empty value to it.

3. Executing Programs with ‘-classpath’ or ‘-cp’ Option

Apart from setting the classpath to the environment variable, you can pass an additional classpath to Java runtime while launching the application using –classpath option or –cp option.

Use the . (dot) to include the current path in the classpath where the .class file has been generated.

$ javac –classpath C:\dependency\framework.jar MyApp.Java $ java –classpath .;C:\dependency\framework.jar MyApp

4. Print Current CLASSPATH Value

Anytime you wish to verify all path entries in CLASSPATH variable, you can verify using the echo command.

//Windows c:/> echo %CLASSPATH% //Linux/Unix $ echo $CLASSPATH

If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) console or simply %CLASSPATH% printed in the windows command prompt.

Источник

Оцените статью
Adblock
detector