Run main of specific class inside a package subdirectory - java

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.

Related

Running java -cp command for a maven project in intellij IDE

I have a maven project in intellij on windows.
Inside, I have a package with a file inside it:
build generates a jar named as 'outputjar':
I want to run java -cp command for the class, and tried various ways:
java -cp \target\outputjar\java\main\ jvm.LongRunningAlloc &
java -cp \target\outputjar jvm.LongRunningAlloc &
java -cp absolute-path-to-jar jvm.LongRunningAlloc &
But none works.
I always get:
Error: Could not find or load main class jvm.LongRunningAlloc
What is correct way of doing it?

Run a executable JAR file from the command line with Classpath

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?

How to include an external JAR in a directory within the Java classpath and run the Java class?

I have a Java file that contains an external JAR and I want to run it. It is a simple project: https://github.com/eveningstar33/external-jar and I tested it with Java 8 and also with Java 11. The command to create the class file is javac -cp ./lib/* HelloWorld.java and it's working, but if I try to run the class using this command: java -cp ./lib/*:./ HelloWorld it doesn't work and I get this error message:
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
What should I do to run it? Thank you!
Use java -cp ./lib/* HelloWorld to run the class.
Update:
If the one given above doesn't work, please try with a "" i.e. use java -cp "./lib/*" HelloWorld to run the class.
Another update (tested):
Sorry for posting earlier solutions without testing them myself. Now, I have tested the following to be working:
java -cp .:lib/* HelloWorld

How to include JAR file in a java program that is being executed by command line (Windows)

I know that there are similar posts to this, but I tried all suggested answers there and they didn't work out (maybe I did something wrong). I have a java project with the following folders:
bin
src
in the bin folder, I have few folders with .class files in them (compiled the project with eclipse, now just need to run it with command line). These are the folders inside bin:
client
game
protocol
threadPerClient
tokenizer
I also added the jar file (gson-2.2.1) to the bin folder. My main class is at threadPerClient folder, and this class name is MultipleClientProtocolServer. The main class get an int as a parameter. These are the commands that I tried to run via command line, from bin folder:
java -cp gson-2.2.1.jar threadPerClient.MultipleClientProtocolServer 4000.
And I got for that:
Error: Could not find or load main class threadPerClient.MultipleClientProtocolServer
java -cp threadPerClient/:./ MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class MultipleClientProtocolServer
java -cp threadPerClient;. com.example.MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class com.example.MultipleClientProtocolServer
What am I doing wrong?
Make sure you're running command 1. of your question from within the bin folder. Also make sure in -cp you specify the gson jar as well as the current folder:
Unix:
java -cp gson-2.2.1.jar:./ threadPerClient.MultipleClientProtocolServer 4000
Windows should be:
java -cp gson-2.2.1.jar;. threadPerClient.MultipleClientProtocolServer 4000
but I cannot test the command for windows since I don't have a windows machine.

Why can I not execute my java project from a script?

I am working on a project that has many module jar files, one of which conains my main class; I am trying to write a shell script that will setup the class path and then start the application; here is my script.
#!/bin/sh
java -cp "modules/*;lib/*" com.example.Launcher
In this example, com.example.Launcher is the class that contains public static void main(String[] args)...
The issue that I am facing is that when executing my script by ./myscript I am give the output
Error: Could not find or load main class com.example.Launcher
This would be simple, there is something wrong with the classpath right?
But if directly from the command line I execute
java -cp "modules/*;lib/*" com.example.Launcher
the exact same command from the script, in the directory the script resides, everything works just fine.
Any thoughts?
side note
I am running this via CygWin
another side note
This might be an issue with sh in CygWin. I coppied this build to a CentOS machine and tried executing it, changing the ; to a : allowed for execution from the script.
The issue persists on my window machine even with the change.
a third sidenote
It would again appear that the issue is with sh in CygWin, my final solution was this:
launcher.sh will invoke java using a : in the classpath
launcher.bat will invoke java using a ; in the classpath
On Unix-like systems the seperator is a : (not a ;). Change
java -cp "modules/*;lib/*" com.example.Launcher
to
java -cp "modules/*:lib/*" com.example.Launcher

Categories

Resources