How to run jar file with -D and -classpath options in Linux - java

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"

Related

NoClassDefFoundError running Java terminal command using the "-jar"

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

Java runs OK from command line, but gives "Could not find or load main class" from shell script

The command below runs fine when I execute it on RedHat 6 from command line:
java -cp /my/dir/Dep1.jar:/my/dir/Dep2.jar:/my/dir/MainJar.jar one.of.my.classes.Class1 <args> > log.txt
However, when I execute the following script test1.sh (which has 755 permissions):
#!/bin/sh
export JEXE="java"
export JBDIR="/my/dir"
export ARGS="<args>"
export JARS="${JBDIR}/Dep1.jar:${JBDIR}/Dep2.jar:${JBDIR}/MainJar.jar"
export CLASSPATH=""
for f in `ls ${JBDIR}/*.jar`
do
export CLASSPATH=$CLASSPATH:$f
done
cd "${JBDIR}"
"${JEXE}" -cp "${JARS}" one.of.my.classes.Class1 "${ARGS}" > log.txt
as ./test1.sh from command line, I get Could not find or load main class. What am I doing wrong?
UPDATE:
Sorry for bad editing of the original question. Corrected.
echo produces java -cp /my/dir/Dep1.jar:/my/dir/Dep2.jar:/my/dir/MainJar.jar one.of.my.classes.Class1 <args>
I added building CLASSPATH trying to get the script to work. Removing it has no effect,i.e. I get the same exact error.
Is it possible that the JAR is build somehow incorrectly? I generate it by running an ant build from Eclipse.
The script worked after I added
export PATH=$PATH:<path to java directory>

Java make batch file for multiple package structure?

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

compile files from different directories with javac, referring a depending jar file?

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.

Java execute jar which depends on other jar from command line

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.

Categories