I am trying the following:
java -cp <path to the additional required jar > -jar <jarname>.jar
I am still getting a java.lang.NoClassDefFoundError when running the above command.
Looks like it still cannot find the external jar mentioned after -cp.
Is that the correct syntax while giving the java command?
You can't use the -jar and -classpath options together. If you want to use the -jar option you need to add the second JAR file to the Class-path attribute in the manifest of the first JAR file.
You could add the additional jar to the bootclasspath
java -Xbootclasspath/a:additional_required.jar -jar main.jar
example
Foo.java
package foo;
public class Foo {
public static void main(String[] args) {
new bar.Bar();
}
}
Bar.java
package bar;
public class Bar {
public Bar() {
System.out.println("foobar");
}
}
manifest.mf
Main-Class: foo.Foo
Execute the commands
javac -d . Bar.java Foo.java
jar cf Bar.jar bar/
jar cmf manifest.mf Foo.jar foo/
java -Xbootclasspath/a:Bar.jar -jar Foo.jar
output
foobar
Given the following command you provide:
java -cp <path to the additional required jar > -jar <jarname>.jar
it will fail because the 'classpath' value must be a ';' separated value. So try adding a ; after your classpath values. For example:
java -cp A.jar;Bjar; -jar <jarname>.jar
and it is mandatory even if you have only one jar file in you classpath string:
java -cp A.jar; -jar <jarname>.jar
Good Luck.
Related
How can I compile and run a runnable JAR file using only javac and jar? I don't want to build the JAR file with an IDE or with tools like Gradle, Maven, or Ant, because I want to understand, for my own edification, how I can compile a runnable JAR using only javac and jar, and then how to correctly run this JAR with java.
I have tried building the JAR like this:
$ javac Example.java
$ file Example.class
Example.class: compiled Java class data, version 55.0
$ jar cvf Example.jar Example.class
added manifest
adding: Example.class(in = 682) (out= 456)(deflated 33%)
$ file Example.jar
Example.jar: Java archive data (JAR)
Then I tried various ways to execute the JAR, but none of them worked:
$ java -jar Example.jar main
no main manifest attribute, in Example.jar
$ java -cp '.;Example.jar' main
Error: Could not find or load main class main
Caused by: java.lang.ClassNotFoundException: main
$ java -cp '.;Example.jar' Example
Error: Could not find or load main class Example
Caused by: java.lang.ClassNotFoundException: Example
$ java -cp '.;Example.jar' -jar Example.jar Example
no main manifest attribute, in Example.jar
$ java -cp '.;Example.jar' -jar Example.jar main
no main manifest attribute, in Example.jar
My code, just to show I have an Example class and a main() function:
import java.util.ArrayList;
import java.util.List;
public class Example {
private static class A {
public int x = 7;
}
public static void main(String[] args) {
List<A> list = new ArrayList<>();
A a = new A();
list.add(a);
a.x = 5;
System.out.println(a.x);
System.out.println(list.get(0).x);
}
}
My javac, jar, and java tools are all version 11.0.15.
I tried looking at the answers on Compile and run with javac and java using classpath classes in jar and javac compiles files and jars but java fails, but none of the suggestions in the answers to these two questions worked for me.
goose#t410:/tmp$ javac Example.java
goose#t410:/tmp$ jar cvfe example.jar Example Example*.class
added manifest
adding: Example$A.class(in = 293) (out= 233)(deflated 20%)
adding: Example.class(in = 682) (out= 457)(deflated 32%)
goose#t410:/tmp$ java -jar example.jar
5
5
Note the penultimate argument to jar is the name of the main class
Thanks to #f1sh and #pdem, I found an answer:
$ javac Example.java
$ jar cfe Example.jar Example Example.class 'Example$A.class'
$ java -jar Example.jar
5
5
Is #f1sh points out in there comment, I needed to (A) include the Example$A.class class when creating the JAR, and (B) run the java command with the correct arguments.
As for making the JAR executable, a manifest file is required. A manifest file can be created manually, or you can let jar create it for you with the e option. The general format for using the e option is
java cfe <name of jar> <name of main class> <list of .class files>
For more info on using the e option or writing a manifest file by hand, see Setting an Application's Entry Point or one of the other child pages of Working with Manifest Files: The Basics.
I have a (Windows 10, Oracle SDK Java-12) Java program in a jar that uses a utility in another jar in a different directory.
I can't run successfully the Java terminal command using the -jar format; only the mainclass format works for me. The utility classes can't be found if using -jar.
I inferred from the Java program help (below) and many other references that -jar program.jar with a manifest entry Main-Class: app.Main would be equivalent to using the app.Main mainclass. It doesn't work if using two different directories so how am I wrong?
Absolute paths to different directories seems to be messing up. There are about a 1000 similar questions answered but I don't see an answer to this detail. (Note I have also tried several experiments using the manifest Class-Path which seems similar in action to the -cp option.)
c:>java
Usage: java [options] mainclass [args...]
(to execute a class)
or java [options] -jar jarfile [args...]
(to execute a jar file)
File c:\AA\program.jar has a manifest with Main-Class: app.Main
File c:\BB\utility.jar
c:\java -cp c:\AA;c:\BB; -jar c:\AA\program.jar
Main loads and gets Exception NoClassDefFoundError for the class in
c:\BB\utility.jar
c:\java -cp c:\AA\program.jar;c:\BB\utility.jar; -jar c:\AA\program.jar
Same as above Main loads and gets Exception NoClassDefFoundError for
the class in c:\BB\utility.jar
c:\java -cp c:\AA\program.jar;c:\BB\utility.jar; -jar program.jar
Unable to access jarfile program.jar (disappointing that it didn't
search classpath but I suppose not unreasonable)
c:\java -cp c:\AA;c:\BB app.Main
Cannot find app.Main (need filename or "*" on classpath)
c:\java -cp c:\AA\program.jar;c:\BB\utility.jar; app.Main
Works okay
I want to add the mysql-connector to my classpath but it does not work with this:
#echo off
java -cp ../lib/mysql-connector-java-5.1.18-bin.jar;../lib/* de/KlickMich/LufthansaAG/test/Main -Xms512M -Xmx1536M -jar test.jar
pause
It comes an Error that the MainClass could not be found or loaded..
Can anyone help me? How do I have to use the [-cp] option?
PS: The MainClass of my java project is de.KlickMich.LufthansaAG.test.Main
There are a number of things wrong with your command line:
java -cp ../lib/mysql-connector-java-5.1.18-bin.jar;../lib/* de/KlickMich/LufthansaAG/test/Main -Xms512M -Xmx1536M -jar test.jar
First of all, you cannot use the -cp and -jar options together. The -jar option is used for running executable jar files, and in that case the classpath is specified in the manifest of the jar file, and not on the command line with the -cp option.
If test.jar is an executable jar, then you specify the classpath and the main class in the manifest file inside the jar, and then you run it with a command like this:
java -Xms512M -Xmx1536M -jar test.jar
Otherwise (if it is not an executable jar file), you have to put test.jar on the classpath, and specify the main class on the command line. In the line above you are specifying a main class the wrong way - do not use slashes (de/KlickMich/LufthansaAG/test/Main), but dots (de.KlickMich.LufthansaAG.test.Main). You have to specify a class name here, not a file name. So, it should be something like this:
java -Xms512M -Xmx1536M -cp ../lib/mysql-connector-java-5.1.18-bin.jar;../lib/*;test.jar de.KlickMich.LufthansaAG.test.Main
I am studying Ant scripts in Java reading this Hello World tutorial: http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
In the previous tutorial it create a new directory by DOS md src command (mkdir in Linux)
then put the following simple code into: src\oata\HelloWorld.java:
package oata;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Now it compile it by this shell statment:
md build\classes
javac -sourcepath src -d build\classes src\oata\HelloWorld.java
java -cp build\classes oata.HelloWorld
I know that javac compile the classess but what exactly do this line?
javac -sourcepath src -d build\classes src\oata\HelloWorld.java
I think that I am sayng to javac that src is where are the sources class to compile, then the -d say that build\classes i the path where to put the compiled class
But what it means the final: src\oata\HelloWorld.java?
Tnx
Andrea
It means the filename(s) to compile.
The purpose of the -sourcepath option is to tell the compiler where the source files for dependent classes may be found. It doesn't imply that everything in that directory should be compiled.
Hi i have been using an IDE but now I need to run and compile from the command line.
The problem is that I have multiple packages and I have tried to find the answer but nothing has worked.
So I have
src/
Support/ (.java files)
Me/ (.java files)
Wrapers/ (.java files)
Do you know how to compile everything with javac?
This should do it (may require additional classpath elements via the -cp command line switch):
javac Support/*.java Me/*.java Wrapers/*.java
But if your build process gets more complex (and it will!), you should look into using Apache Ant for build automation.
You should use build tools like Maven or Ant for such tasks.
In the initial stages, when the project is not very complex you can use the following line to compile, with appropriate classpath in place(as suggested by #Michael):
javac Support/*.java Me/*.java Wrapers/*.java
javac -d compiled $(find src -name *.java)
If you really need to just use javac and standard UNIX commands you could to this:
find src -name \*.java -print0 | xargs -0 javac -d classes
The real answer is javac -d (places where classes to be built and placed) -sourcepath (source of the package at the root) -cp (classpath of the dependencies which can again be classes folder where the classes are build and kept) full qualified name of the java file.
Ex javac -d classes -sourcepath src -cp classes src\com\test\FirstSample.java
The FirstSample.java contains the main method. Pacjage structure mentioned below.
Before compiling
HomeApp
--src
------com\test\FirstSample.java (First Sample using the FirstPojo.java)
------com\test\FirstPojo.java
--classes
After compiling
HomeApp
--src
------com\test\FirstSample.java (FirstSample.java using the FirstPojo.java)
------com\test\FirstPojo.java
--classes
------com\test\FirstSample.class (FirstSample.class using the FirstPojo.class)
------com\test\FirstPojo.class
In many cases Ant is overkill. Just use a BAT file if you are in windows or a shell script (sh file) if you are in linux. You can create a text file which includes all your javac commands and just run that file when you want to build.
For example, I use the following bat file to build one of my apps:
#echo off
echo Building Shazaam...
del classes\com\aepryus\shazaam\*.* /q
del classes\com\aepryus\shazaam\engine\*.* /q
del classes\com\aepryus\shazaam\domain\*.* /q
del classes\com\aepryus\shazaam\persist\*.* /q
del classes\com\aepryus\shazaam\view\*.* /q
del classes\com\aepryus\shazaam\task\*.* /q
del classes\com\aepryus\shazaam\action\*.* /q
del classes\com\aepryus\shazaam\controller\*.* /q
javac src\com\aepryus\shazaam\*.java -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar; -d classes
javac src\com\aepryus\shazaam\engine\*.java -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\domain\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\persist\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\view\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\task\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\action\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\controller\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepRPC.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
cd classes
jar cf ..\war\WEB-INF\lib\Shazaam.jar .
cd..
echo Complete
To compile Run below command [it will store all class files in classes folder]
javac -d classes Support/*.java Me/*.java Wrapers/*.java
**Note : classes folder should be created first
To Run java application, run below command
java -cp classes {mainfile_name}
Replace mainfile_name with your main file.