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
Related
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 trying to run a jar file with the -D option and multiple jar files in the classpath. How can I do that in linux?
Here is my command.
java -cp -Dcom.sun.management.config.file=/usr/local/monitor/jmx-management.properties .:/usr/local/monitor/monitor.jar:/usr/lib/oracle/11.2/client64/lib/ojdbc6.jar com.mypackage.Monitor
When I run this I'm getting an error " Could not find or load main class .:.usr.local.monitor.monitor.jar:.usr.lib.oracle.11.2.client64.lib.ojdbc6.jar"
I am trying to make a batch file to create my client side application window, I am using multiple packages.
src/nu/connect/client/* contains all the logic and chat window itself.
src/nu/connect/message/* contains the MessageStructure class file.
javac -d bin src\nu\connect\client\*.java
javac -d bin -cp bin src\nu\connect\client\ChatWindow.java
java -cp bin nu.connect.client.ChatWindow
pause
Here is the error I am getting,when i run the batch file:
src\nu\connect\client\ChatWindow.java:7: error: package com.message does not exist.
Here is the solution to my problem, had to compile both packages in one line.
javac -d bin src\nu\connect\client\*.java src\nu\connect\message\*.java
javac -d bin -cp bin nu\connect\client\ChatWindow.java
java -cp bin nu.connect.client.ChatWindow
pause
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.