I have an application that uses an external jar. I used eclipse and it works fine. I export as jar from eclipse, having created a Manifest file that has as Class-Path: ./cab.v1.jar
I place both jars in the same directory.
I run in command line:
java -jar myApp.jar
and get java.lang.NoClassDefFoundError for the classes in the cab.v1.jar (the other jar)
Have also tried java -cp . -jar myApp.jar but no success.
What am I doing wrong?
Using the documentation for the Manifest it does not use a ./ for relative directories. Try it just with:
Class-Path: cab.v1.jar
Note that the -cp option is ignored when using -jar.
If you use the -jar option the classpath is ignored. You could start the application by
java -cp jar1.jar:jar2.jar mainclass
The class path separator ':' is ';' on windows.
Related
I have a simple non-modular jar file main.jar that depends on libA.jar and libB.jar. These three jar files all reside in the current directory. I want jdeps (version 15.0.1) to generate a module-info.java file for main.jar. Here's my shell command (using cmd.exe on Windows):
jdeps --module-path . --add-modules=ALL-MODULE-PATH --generate-module-info . main.jar
This command indeed produces the desired module-info.java file but also issues this warning:
Warning: split package: org.example.main file:///D:/Temp/./main.jar main.jar
writing to .\main\module-info.java
The package org.example.main does only exist in main.jar - so I reckon there should be no warning.
Any hints as to why jdeps does see a problem here ?
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
If i want to embed JRE with my application, i have this directory structure.
+---jre
| +---bin
RunMyApp.bat
myApp.jar
i have added this statement in RunMyApp.bat to access the java.exe
jre\bin\java.exe -help
Then how to run the myApp.jar, using RunMyApp.bat script?
i have tried this one, but not succeed
jre\bin\java.exe -jar "%cd%\..\..\myApp.jar"
Add an entry to your MANIFEST.MF (under META-INF directory) for the Main-Class attribute and say jre\bin\java.exe -jar <your-jar-filename>
I have the following set up:
I have 4 packages:
root/src/terminal - has some java files
root/src/mail - has some java files
root/src/data - has some java files
root/src/main - has a single java file, Main.java
I also have the following files
root/bin - a folder to store .class files
root/mail.jar - a jar file which has important classes used in my code
Within the root, I would like to enter a terminal command which compiles root/src/main/Main.java and puts the class files in the root/bin location.
Can someone show me the command to do this? I'm on a Mac (running Leopard).
Here's the one liner:
cd /xyz/root
rm -rf bin/*
javac -d bin -classpath mail.jar -sourcepath src main/Main.java
Alternatively, you could use absolute directory names:
rm -rf /xyz/root/bin/*
javac -d /xyz/root/bin -classpath /xyz/root/mail.jar \
-sourcepath /xyz/root/src /xyz/root/ main/Main.java
In reference to Ant you said "I would rather keep it simple.".
In fact in the long term it is simpler to create a simple Ant build.xml file. The alternative is a bunch of non-portable scripts or batch file ... or lots of typing.
To run the application, assuming that you are still in the /xyz/root directory:
java -classpath bin:mail.jar main.Main
Or on Windows:
java -classpath bin;mail.jar main.Main
Or modify the above to use absolute pathnames in the classpath argument; e.g.
java -classpath /xyz/root/bin:/xyz/root/mail.jar main.Main
Without knowing your operating system?
What you should look into is using Apache Ant. It is a build tool that once installed and configured can utilize a build.xml file in your root to compile class files to a folder as well as package a jar file.
http://ant.apache.org/
try this:
javac -cp "/root/mail.jar;/root/src;" -d "/root/bin" Main.java
This is written hoping that you have package declarations in your classes from src folder like package terminal; and package main;.
See this: Options in javac command
Or use Apache Ant as suggested by maple_shaft.
From comment give by #maple_shaft:
In Unix, Linux operating systems the classpath separator is a colon instead of a semicolon.