Specify Java CLASSPATH variable [duplicate] - java

This question already has answers here:
Specifying classpath for a jar
(5 answers)
Closed 8 years ago.
I am not familiar with Java and I am trying to run a piece of Java code packaged as jar file.
On Windows command line, I specify the CLASSPATH to my jar folder like this:
set CLASSPATH="D:\jarFolder"
And there's a test.jar file in that folder. But when I run this
java -jar test.jar
it still failed with this error:
Unable to access jarfile test.jar
I can run the test.jar by specify the full path. But I want to know why the CLASSPATH doesn't work. My understanding is, it tells the java runtime where to locate the jar file.

Jarfiles have to be on the classpath explicitly. Specifying a classpath dir for a jarfile is not sufficient.
See the related Oracle docs on the Java command line:
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.

Try to use
java -jar D:\jarFolder\test.jar
If you know main class name of your entry point of jar. You can use
set CLASSPATH="D:\jarFolder"
java yourpackage.yourEntryPoint
You can learn your entry point of your jar file using a zip program to extract jar file and reading file entry point section of manifest.mf file.
When you use -jar option, you need to specify which jar. Think of it this way. If you do not give jar name, which jar java should execute? You may have 10,20 may be 100 jars in your class path.
But if you give class name, then java searches among jars and execute correct one.

Related

How to make a .jar file that runs in the command prompt [duplicate]

This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 6 years ago.
I am learning Java, and I have been programming an 80s-styled text based game. I have experience with coding so I know I will be done soon. Just so I knew how, I decided to make a .jar file. It created fine, but I have a problem: I want it to run and work in the command prompt, just like if I were using the "java FileName" command. Is there any way to do this? I want to distribute it so that for Windows, it runs in the command prompt exclusively (Apple and Linux is down the line).
Edit/Summary: To some people, this might be a duplicate of a question. It's not for these simple reasons: I know how to create a .jar file. I am trying to figure out how to create a specific type of .jar file that opens and runs in the command prompt.
I assume that you have compiled your .java files into class files. If so, than do the following:
Make a manifest file called manifest.txt and inside the file mention the class name where the main class resides. For example:
Main-Class: com.A.B.MyMainClass
com.A.B.MyMainClass is example here. You should mention your package names.
Put the above line in the manifest.txt file
Than, suppose you have your class files in c:\test\classes\ folder and suppose your class files in this folder are a.class and b.class and MyMainClass.class
Now execute the command:
jar -cvfm MyExecutable.jar manifest.txt c:/test/classes/*.class
It will create an exectable jar for you which you can run in Windows and Mac and also in Linux.
To execute the jar file:
java -jar MyExecutable.jar in the command prompt.
Use this to run your jar on command line:
java -jar <jar-name>.jar
Assuming that you have java installed on the system
Using eclipse -
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm
Using Command line -
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
To run the jar file -
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Adding new libraries to java [duplicate]

This question already has answers here:
Including all the jars in a directory within the Java classpath
(25 answers)
Closed 7 years ago.
I am trying to use a feature from the Apache Commons called StringUtils. However this requires you download the libary and add it so I can use the code import org.apache.commons.lang3.StringUtils;. My problem is I am unsure of where to add it to so that I may compile my program in command prompt. I am also unaware of what file I should to add into the required folder.
Any help would be greatly appreciated.
You'll have to make sure that the library (usually a JAR file) is in the classpath when you compile and run your application that uses the library.
The classpath is the set of JAR files and directories that Java uses to find Java class files.
See PATH and CLASSPATH in Oracle's Java Tutorials. (This is about setting the environment variables PATH and CLASSPATH).
As an alternative to setting the CLASSPATH environment variable, you can use the -cp or -classpath options of the javac and java commands:
javac -cp C:\MyProject\lib\somelibrary.jar;. com\mypackage\MyProgram.java
java -cp C:\MyProject\lib\somelibrary.jar;. com.mypackage.MyProgram
If you are not using any IDE, then include below line in your .java file.
import org.apache.commons.lang3.StringUtils;
then we you compile the .java class from cmd prompt, include the apache commons jar in classpath. like
javac -cp ../commons**.jar your_class.java
You will need the CLASSPATH environment variable to point to it. Link.
Given the context of the question I assume you are not using maven or an IDE. If you are there are simpler ways.

Differences between "java -cp" and "java -jar"?

What is the difference between running a Java application withjava -cp CLASSPATH and java -jar JAR_FILE_PATH? Is one of them preferred to the other for running a Java application? I mean which one of these ways is more expensive for JVM (according to their machine resources usage)?
Which one will cause JVM to spawn more threads while trying to run the application?
I prefer the first version to start a java application just because it has less pitfalls ("welcome to classpath hell"). The second one requires an executable jar file and the classpath for that application has to be defined inside the jar's manifest (all other classpath declaration will be silently ignored...). So with the second version you'd have to look into the jar, read the manifest and try to find out if the classpath entries are valid from where the jar is stored... That's avoidable.
I don't expect any performance advantages or disadvantages for either version. It's just telling the jvm which class to use for the main thread and where it can find the libraries.
With the -cp argument you provide the classpath i.e. path(s) to additional classes or libraries that your program may require when being compiled or run. With -jar you specify the executable JAR file that you want to run.
You can't specify them both. If you try to run java -cp folder/myexternallibrary.jar -jar myprogram.jar then it won't really work. The classpath for that JAR should be specified in its Manifest, not as a -cp argument.
You can find more about this here and here.
PS: -cp and -classpath are synonyms.
When using java -cp you are required to provide fully qualified main class name, e.g.
java -cp com.mycompany.MyMain
When using java -jar myjar.jar your jar file must provide the information about main class via manifest.mf contained into the jar file in folder META-INF:
Main-Class: com.mycompany.MyMain
java -cp CLASSPATH is necesssary if you wish to specify all code in the classpath. This is useful for debugging code.
The jarred executable format: java -jar JarFile can be used if you wish to start the app with a single short command. You can specify additional dependent jar files in your MANIFEST using space separated jars in a Class-Path entry, e.g.:
Class-Path: mysql.jar infobus.jar acme/beans.jar
Both are comparable in terms of performance.
Like already said, the -cp is just for telling the jvm in the command line which class to use for the main thread and where it can find the libraries (define classpath). In -jar it expects the class-path and main-class to be defined in the jar file manifest. So other is for defining things in command line while other finding them inside the jar manifest. There is no difference in performance. You can't use them at the same time, -jar will override the -cp.
Though even if you use -cp, it will still check the manifest file. So you can define some of the class-paths in the manifest and some in the command line. This is particularly useful when you have a dependency on some 3rd party jar, which you might not provide with your build or don't want to provide (expecting it to be found already in the system where it's to be installed for example). So you can use it to provide external jars. It's location may vary between systems or it may even have a different version on different system (but having the same interfaces). This way you can build the app with other version and add the actual 3rd party dependency to class-path on the command line when running it on different systems.
There won't be any difference in terms of performance.
Using java - cp we can specify the required classes and jar's in the classpath for running a java class file.
If it is a executable jar file . When java -jar command is used, jvm finds the class that it needs to run from /META-INF/MANIFEST.MF file inside the jar file.

Java Classpath problem

I have a Java Program that references a Jar File.
The Jar File is present in the same directory as the .class File but when I try to run the program from console, I get NoClassDefFound error :-(
Alternatively it runs Ok from Eclipse.
Why ?
The java program will not automatically include JAR files in its class path. You add a JAR file to its class path via the -classpath option. For example:
java -classpath .;yourJarFile.jar your.MainClass
Eclipse is automatically adding the JAR file whenever you run the program.
Note: The -classpath option expects a semicolon (;) when running on Windows and a colon (:) otherwise.
JAR files are not automatically included in the classpath. You can add a Class-Path entry and a Main-Class entry to the to JAR file containing the main method that you wish to execute. Then you can execute your code like so:
java -jar yourJarFile.jar
See the JAR File Specification for more details.
or specify the classpath on the command line:
java -classpath lib1.jar:lib2.jar:yourJarFile.jar your.MainClass

The dreaded java.lang.NoClassDefFoundError

I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth

Categories

Resources