Run a executable JAR file from the command line with Classpath - java

I have a Spring boot jar which I can run using the below command.
java -jar sample.jar
--spring.config.additional-location=application.properties
But, When I run the same jar with -cp, getting unable to find Main class error.
java -cp /usr/test/libs/* com.test.Sample
--spring.config.additional-location=application.properties
Not sure why it is not able to load the main class with this way?

Related

How to include a jar file in classpath and run another jar in linux

I am trying to run a jar file on Linux but it requires another jar file to run. To be specific I have added ojdbc8 separately in Eclipse as I am unable to add it in pom.xml. In Eclipse it works fine but in Linux when I try to run the command
java -cp "/home/eim_master/EirRefresh/lib/ojdbc8.jar" -jar EirRefresh-0.0.1-SNAPSHOT.jar
even after including ojdbc8 jar it still fails throwing
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
I need to run the main class and start the spring boot application
The -cp option is for a directory.
Try java -cp "/home/eim_master/EirRefresh/lib/" -jar EirRefresh-0.0.1-SNAPSHOT.jar
I tried this and it worked for me
java -cp "target/EirRefresh-0.0.1-SNAPSHOT.jar:lib/ojdbc8.jar" org.springframework.boot.loader.JarLauncher

Run main of specific class inside a package subdirectory

I am following Scheduled Jobs with Custom Clock Processes in Java with Quartz and RabbitMQ but I struggle to actually run another dyno from withing the jar file packeg by Spring.
In the package (server-1.0-SNAPSHOT.jar) I need to run the company.server.Scheduler. The .class file is in BOOT-INF/classes.
I've tried to do this but I am always getting
Error: Could not find or load main class company.server.Scheduler
I'm struggling to get the syntax right.
So what I need to run is
BOOT-INF/classes/company/server/Scheduler.class
I have tried this:
java -classpath BOOT-INF/classes -jar server-1.0-SNAPSHOT.jar company.server.Scheduler
java -classpath server-1.0-SNAPSHOT.jar:/BOOT-INF/classes company.server.Scheduler
But this either runs the main class from the Manifest or crashes.
Also tried:
java -classpath server-1.0-SNAPSHOT.jar BOOT-INF.classes.company.server.Scheduler
java -classpath server-1.0-SNAPSHOT.jar BOOT-INF/classes/company/server/Scheduler
Try it out ...
git clone https://github.com/silentsnooc/run-scheduler
cd run-scheduler/
mvn clean install
cd target/
java -cp .:scheduler-test-1.0-SNAPSHOT.jar BOOT-INF.classes.Scheduler
If I follow this on Heroku then it should be something like
java -cp scheduler-test-1.0-SNAPSHOT.jar:BOOT-INF/classes/* Scheduler
but it's not working telling me the main class Scheduler could not be found.

java -jar and java.lang.NoClassDefFoundError

root#raspberrypi:/home/pi/java/src/driver# make run
java -jar Core.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
I used and altered a makefile that I'm sure of is correct, but let me include it as well:
http://pastebin.com/nzenp2E8
What do I do wrong?
Edit:
I know the imports are correct
And I've already tried like giving java -jar Core.jar a classpath in several ways, but the same error keeps popping up.
Create jar with -e option [specify application entry point for stand-alone application bundled into an executable jar file]:
jar cfe Core.jar pract2.Solution pract2\*
Here pract2.Solution is main class.
Run using
java -jar Core.jar
Or
java -cp .;Core.jar -jar Core.jar

Run jar file from unix command prompt (ERROR)

I have a java project that has 10 .java files. A1.java, A2.java......A9.java and Main.java.
The project contains a package grand.big.medium.small and all the .java files are inside this package.
I have compiled and created an executable jar file for the project using:
javac grand/big/medium/small/*.java
to run the project I used the command
java -cp . grand/big/medium/small/Main
to create the jar I used the command
jar -cvmf manifest.txt MyJAR.jar *.class
I want to run the jar file from command prompt and I am using the commad
java -cp . -jar grand/big/medium/small/MyJAR.jar
and I getting an error:
Error: Could not find or load main class Main
please can anyone tell me why I am getting this error?
Run as:
java -cp . -jar grand/big/medium/small/MyJAR.jar grand.big.medium.small.Main
Assuming you have grand as a directory within current directory and directory hierarchy follows.

Run a jar using a shell file and specify classpath and properties file path

I've compiled a Spring Maven project JAR. My dependencies are all in a directory labeled lib. How to execute my JAR while specifying it should use the LIB/* as the classpath and also use log4j.xml and a properties file called marker.properties. My marker.sh
LIB=$INSTALL_PATH/lib
echo "$LIB" # outputs usr/local/app/lib
export CLASSPATH=$(JARS=("$LIB"/*.jar); IFS=:; echo "${JARS[*]}")
PARM="-Dlog4j.configuration=/usr/local/Marker/log4j.xml"
java $PARM -jar /usr/local/Marker/marker-1.0.jar
But on running this I get the following error as it is not able to find the jars in the classpath
Exception in thread "main"java.lang.NoClassDefFoundError: org/springframework/context/ConfigurableApplicationContext at java.lang.Class.getDeclaredMethods0(Native method)
However, this command is able to load the jar from classpath, but I am unable to add the properties file
java -cp /usr/local/Marker/marker-1.0.jar:"$LIB"/* com.package.Marker
java -cp /usr/local/Marker/marker-1.0.jar:"$LIB"/* com.package.Marker worked perfectly fine. I needed to add .properties file and log4j.xml in folder where jar was placed
You can specify the classpath using the -classpath (or -cp) flag; for example:
~$ java -jar package.jar -classpath /path/to/classpath/*
For more information, see the following documentation.

Categories

Resources