This seems to me to be a trivial question, but I've had a lot of trouble getting an answer.
I've developed a project in eclipse that is dependent on a jar file, which resides in the project's root directory. All my files are in a package "a.b.c" in a src folder. It runs just fine in eclipse. I now want to run this project from the command line. I do this command to compile the project:
javac -classpath dependency.jar -d ./bin/ ./src/a/b/c/*.java
Everything is compiled into class files and put into the bin/a/b/c folder. Then I do these commands to run the project:
cd bin
java -cp ../dependency.jar a.b.c.Main
Now I get "java.lang.NoClassDefFoundError: a/b/c/Main".
So, how do I run a project that is in a package and depends on a jar file?
Just include the current dir in the classpath as well - i.e. java -cp ../dependency.jar:. a.b.c.Main
You also need to specify your compiled files on the classpath, these will contain your a.b.c.Main. On *nix flavor machines the path separator for cp is the colon (:), and on windows it's a semicolon (;), so on *nix, your run command should be (because you're running from the bin directory):
java -cp ../dependency.jar:. a.b.c.Main
Related
I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it should use the lib/* as the classpath.
I've tried:
]$ java -jar -cp .:lib/* MyJar.jar
]$ java -cp .:lib/* -jar MyJar.jar
]$ java -cp .:lib/* com.somepackage.subpackage.Main
etc...
Each gives an error saying:
Error: Could not find or load main class ....
or gives the NoClassDefFoundError indicating the libraries are not being found.
I even tried remaking the JAR file and included the lib directory and contents, but still no dice...
How can I execute a JAR file from the command line and specify the classpath to use?
When you specify -jar then the -cp parameter will be ignored.
From the documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
include all jar files from the lib directory into the manifest (you can use relative paths there)
Specify everything (including your jar) on the commandline using -cp:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Run a jar file and specify a class path like this:
java -cp <jar_name.jar:libs/*> com.test.App
jar_name.jar is the full name of the JAR you want to execute
libs/* is a path to your dependency JARs
com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method
The jar and dependent jar should have execute permissions.
You can do these in unix shell:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
You can do these in windows powershell:
java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main
Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:
Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar
Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.
Then execute it with
java -jar MyJar.jar
You can do a Runtime.getRuntime.exec(command) to relaunch the jar including classpath with args.
I am using Java8. I have one .jar file containing .class files.
I downloaded the source code of the same .jar file. And I edited one of the file in it. It is .java file.
Now I want to compile all these .java files in the source code and create a new .jar file with .class files
Any clues?
There are many options
1. if you want to do it from command prompt then you would need to set the classpath and then either create a list of java files with package name and use it for compiling, something like this
# Linux
$ find -name "*.java" > source.txt
$ javac -classpath "${CLASSPATH}" #source.txt
:: Windows
> dir /s /B *.java > source.txt
> javac -classpath %{CLASSPATH}% #source.txt
or use build tool like Ant.
Once you have the .class files, you can use "jar" command to create a jar of the .class files
2. Use IDE - you can use any of these IDEs - eclipe, intellij, netbeans.
You need to setup a project with the java files and compile project and export as jar using class files.
I think it would work out better for you to use an IDE.
1) Compile the classes (javac command)
If you don't have any jar dependencies required at compile time, you should not need to set the classpath.
Otherwise you should specify the cp argument with.
Example (Unix way) :
javac -cp "myLib.jar:myOtherLib.jar" *.java
2) Then create a jar from these compiled class (jar command)
Example :
jar cf myjar myFolderWithCompiledClass
extract your jar to a folder and run this:
javac [folder]/*.java
read that for more info:
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
So, I have exported my project in both Netbeans and Eclipse and when I try to
java -jar myproject.jar
I get this prompt
In my project I have some libraries which are located inside of src in Netbeans and out of src in Eclipse as it should (please correct me if I'm wrong) The libraries are included via:
Java Build Path > Add JARs...
I've done some research and it seems that I have to change my JAVA CLASSPATH or somethng like that but I don't know exactly how to do it.
The project works perfectly when I compile it and run it, but it crashes after I build it into a Jar file.
By the way, if it isn't clear enough I'm on Ubuntu 14.04
You need to create the path for the jar files and pass it on the command line.
Something like this:
ftp_jar=${Utils_home}/bin/ftpClientUtil.jar
net_jar=${Utils_home}/bin/commons-net-3.3.jar
jsch_jar=${Utils_home}/bin/jsch-0.1.51.jar
java -cp .:$jsch_jar:$net_jar:$ftp_jar com.myplace.ftputils.SFTPClientUtil $*
Run your program as:
java -cp .:[path-of-lib1.jar]:[path-of-lib2.jar] -jar myproject.jar
replace [path-of-libX.jar] with actual path of your libraries.
I need to use 'JDatePicker' class for a program. I downloaded 'jdatepicker-1.3.2.jar' file. and when i extracted the file using the command "jar xf jdatepicker-1.3.2.jar", I got 3 folders in the directory " META-INF, tutorial, net". But i don't know how to import this to the java program and where to store this jar file and the extracted folders. could any one assist me on the above?
I don't use any IDE. I run the java programs in command prompt
You must have that exact jar file on the class path. When compiling specify jar in the compile
javac -classpath jars\jdatepicker-1.3.2.jar -sourcepath src\project -d classes src\project\MyProg.java
I have four java files in my folder. They are all in the same package. Here's the package declaration
package com.osama.GHide
All of these classes are in the same package. I want to know how can I compile them using javac (i mean i do not know how to compile multiple files that are using each other). And once that is done how do I launch then using java command in the CLI? here are the file names.
EnteringPoint.java
HidingProcess.java
ListFiles.java
From the project's root directory:
javac src/com/osama/GHide/*.java
To run, assuming no other dependencies:
java -cp ./src com.osama.GHide.EnteringPoint
(Assuming EnteringPoint has the normal main function.)
The javac command compiles all the .java files in the package's directory. Since they're all in the same package/directory, this works. It also puts the generated .class files in the same directory, which may or may not be what you want.
To put them in a different directory, use the -d option and supply a path.
javac -d bin src/com/osama/GHide/*.java
Then to run:
java -cp ./bin com.osama.GHide.EnteringPoint
You can run by using command :- javac -cp src/com/**/*.java
I am asuming there is no other dependenices and your root class name would be src only.