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
Related
I want to run a .sh file named ubiqstart.sh. using java. Specifically, I just want to execute a java class when running this .sh file. But I keep getting this error: Error: Could not find or load main class com.ubiq.update.MainEntry. Seems like the system is not able to locate where the java class is at. However, when I directly type in and execute this command in terminal (under this exact directory), it runs perfectly.
The script I use in the .sh file is: "../java/bin/java" -cp "./*" com.ubiq.update.MainEntry.
I wonder what causes this conflict and what should I do to make my .sh file to do the same thing as directly running the script.
When you are using older editors like vim or nano, just simply add a space after your classpath, like: com.ubiq.update.MainEntry . Especially if you convert the script from windows' .bat to Linux .sh
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.
(Crossposting note: I have asked this question one week ago at the JRuby mailing list, but didn't get any answer yet).
I have a jar file provided by someone else, no access to the source code. The jar file is in lib/other/appl.jar, the class is named Appl, and the package is com.abc.xyz
I would like to instantiate an Appl object from the JRuby irb, jirb_swing_ex.
(Of course my problem applies not only to jirb, but to running JRuby programs in general, but I explain it in the way I'm using it right now, just in case there are some peculiarities in Jirb which need special treatment).
THIS is the way it DOES work:
(1) Invoke jirb by:
java -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
(2) Put the directory with the jar file into the load path:
$: << 'lib/other'
(3) Load the jar file
require 'appl.jar'
(4) Import the class
java_import com.abc.xyz.Appl
(5) Create the object
x = Appl.new
As I said, this works, and I can live with it if necessary, but I would prefer a simpler approach:
NOW TO MY QUESTION: Instead of fiddling around with load path and doing a require for the Jar file, I thought I could let Java already include the jar file. This is what I have tried:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
The problem is: How can I get at my object? If I just use the class name com.abc.xyz.Appl, JRuby complains that the class not found (NameError: missing class name).
BTW, I have also tried forward slashes (since I'm on Windows), i.e.
java -cp lib\other\appl.jar -jar jr\jruby-complete-1.7.27 jb\jirb_swing_ex
but the same effect. I had expected that, having appl.jar in my class path, would make the classes available somehow, but I seem to miss something.
Running jirb or jirb_swing with custom class path
jirb and jirb_swing will use the value of JRUBY_CP environment variable (if present) to extend class path given to Java command line.
Example with commons-lang3 library taken from my local maven repository, using bash on Linux or macOS:
$ export JRUBY_CP=${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar
$ jirb
irb(main):001:0> org.apache.commons.lang3.mutable.MutableBoolean.new
=> #<Java::OrgApacheCommonsLang3Mutable::MutableBoolean:0x7c24b813>
Running JRuby programs with custom class path
To run a JRuby program that uses a third-party java library, this won't work:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 ...
You must use either -jar or -cp, you can't combine the two.
From java man page:
When you use this option [-jar], the JAR file is the source of all user classes, and other user class path settings are ignored.
In addition, you need to pass the main Java class, which is org.jruby.Main, and that class needs arguments: either a path to a Ruby script, or other command line arguments such as -e 'puts 2+2'.
So the command line structure is the following:
# Run script file:
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main path/to/script
# Run simple one-line Ruby program
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main -e 'some ruby here'
(on Windows please use ; instead of : as separator)
Actual example with same commons-lang3 library & OS:
$ alias myjruby="java -cp ${JRUBY_HOME}/lib/jruby.jar:${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar org.jruby.Main"
# Verifying base jruby code works with that:
$ myjruby -e 'puts 2+2'
4
# Now verifying it can use my 3rd-party lib:
$ myjruby -e 'puts org.apache.commons.lang3.mutable.MutableBoolean.new'
false
There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.
I've a java script who's running by several user and working very well.
Today, I asked another user to try the script on his desktop and he's getting a ClassNotFoundException... despite the script is perfectly the same as mine (and jar locations is also the same)
Here's the command tu launch the JAR :
java -cp .;customname.jar;libs/* my.package.MyMainClass
And I also tried to add every jar in the libs folder separately :
java -cp .;customname.jar;libs/lib.jar;libs/lib2.jar;libs/lib3.jar my.package.MyMainClass
And here's the error message the user is getting :
Error: Could not find or load main class ch.vaudoise.hp.services.listener.AutoSysReorder
I checked the JAVA configuration and try to set him the same java version on "Path" environment variable. Same error.
As there's 6 user who can run the script and only one who's getting an error I'm sure it's a configuration issue. But what ? Classpath seems to be OK...
Many thanks for any help..
First things first: You must start by finding where is the conflicting class. If you don't know it, you may find it in two alternative ways:
Programatically: Code this class and execute it with the same classpath (on an environment that does not suffer the problem):
public static void main(String[] args)
{
System.out.println(ch.vaudoise.hp.services.listener.AutoSysReorder.class. getResource("/ch/vaudoise/hp/services/listener/AutoSysReorder.class"));
}
Manually, one by one: Open a command shell and execute:
javap -cp . ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp customname.jar ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp libs/lib.jar ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp libs/lib2.jar ch.vaudoise.hp.services.listener.AutoSysReorder
...
Try one by one every entry in the classpath until the class is found.
Once found the location of the class, open a shell in the conflicting PC and make sure that path is accessible:
dir lib\conflicting-library-or-directory
Also, repeat the javap test:
javap -cp conflicting-library-or-directory ch.vaudoise.hp.services.listener.AutoSysReorder
After this tests, you should have more clues to find the cause of the problem.
Take a look at ClassNotFoundException despite class in the classpath
You are also including meta character (*) in your classpath.
Try without that as suggested in the link.
Also some times copy pasting to command line , may get some characters copied differently.
I cannot add comment as of now : So , editing this answer.
| Java path is not an issue.
Try the following step by step :
- find which jar the class that is being not found is in .
- include only that jar as cp.
- include only that class and try
java -jar that.jar
Also try this once
java -cp "*;"
If you still got issue , probably the jar does not contain the class (You can open jar and check).
And you say script - is this single command which is failing or is it part of script ?. Using java -jar -cp , usually ignores cp.