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
Related
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've got the following script to run:
# harvest_bug
#
start on runlevel [345]
script
java -jar /home/admin/es09AndroidUpdater/es09AndroidUpdater.jar
end script
But my linux machine doesn't know anything about java. If I run java -version, I will get
bash: java: command not found
I guess java is not in $PATH, because I still can run Tomcat and things like that. For example Tomcat's setenv.sh looks like that:
export JAVA_HOME=/usr/java/default
export JRE_HOME=/usr/java/default/jre
PATH=$PATH:$JRE_HOME/bin
export PATH
PATH=$PATH:$JAVA_HOME/bin
export PATH
export JAVA_OPTS="-Des09.config=/home/es09/es09.properties -Xms128m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"
So how can I modify the script to run my jar? Can I do this?
# harvest_bug
#
start on runlevel [345]
script
export JAVA_HOME=/usr/java/default
export JRE_HOME=/usr/java/default/jre
PATH=$PATH:$JRE_HOME/bin
export PATH
PATH=$PATH:$JAVA_HOME/bin
export PATH
export JAVA_OPTS="-Des09.config=/home/es09/es09.properties -Xms128m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"
java -jar /home/admin/es09AndroidUpdater/es09AndroidUpdater.jar
end script
Is that ok? Will linux run this script? Sounds very stupid, because I'm not familiar with bash, linux and things like that.
The main purpose of setting java path in $PATH or $JAVA_HOME environment variable is to define the exact path of java executable.
To run your script, use
$JRE_HOME/bin/java $JAVA_OPTS -jar /home/admin/es09AndroidUpdater/es09AndroidUpdater.jar
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.
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.
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.