I built a spring-boot application and make the jar file exectuable. There is a compileOnly dependency on my project which need to be provided at runtime. When I run java -jar myApp.jar I will get ClassNotFound exeception which is expected. But I don't know how to add the additional jar file on java command. I have tried below command:
java -Dloader.path=/libs/third.jar -jar myApp.jar
but it doesn't work. How can I add /libs/third.jar on my application?
On Unix:
java -cp MyApp.jar:./libs/third.jar com.packagename.MainClass
On Windows: use ; instead of : and also \ instead of /
After some searching I figured out the problem with -Dloader.path. In order to make it works I need to change the project layout to be ZIP which will use PropertiesLauncher. Below is the configuration.
springBoot {
executable = true
layout = "ZIP"
}
Related
I am not able to run a spring boot application (main class) from UNIX using putty with could not find or load main class error.
All files are given 0755 executable permissions under the project folder. Tried using command:
java -cp .:batch-services.jar:lib/* com.spring.integration.demo.SpringBootDemoApplication
Running command from the path: /app/batch
Folder structure in UNIX:
/app/batch/lib - this folder has all the dependency jars
/app/batch/batch-services.jar
Expected result is that the spring boot application will start successfully.
Actual result is:
Error: Could not find or load main class com.spring.integration.demo.SpringBootDemoApplication
It looks like you have encountered a common issue with how Java interacts with shell wildcards (asterisks). Java expects your classpath elements to be separated by colons, but your shell generates spaces.
The solution is to quote the argument. See this answer: Including all the jars in a directory within the Java classpath
Also, if you are using spring-boot, you can build your application into a so called fat-jar.
If I extract a Jar file into a directory, what's the correct java command to run it as if it was the jar?
Meaning, what would be the equivalent command to java -jar myProgram.jar except not using the jar itself?
I've tried stuff like java -cp ".;META-INF\lib\*" JarMain but it seems like there's still something missing there.
Do you mean that add a jar to classpath but do not run it? If so, java -cp myProgram.jar;ohterjar/libdir JarMain. If you use linux/mac,replace ; to :.
If you run jar file directly (without extracting), you can use command: java -jar jarfilename. jar
If you extract jar file into separated files, you can run .class file only. Use the command java classfilename.
You need to set path environment to java/bin directory before running above commands, or just change directory to it.
I would like to know can I run a jar file from the command, with the jar file using log4j and ojdbc.jar as well.
The 'main' is located in: nmap_logic.jar.
Within the package containing the 'main' is called: "nn.gmap.logic".
I also use 2 external jar files: log4j.jar & ojdbc.jar.
I have tried running:
java -cp "nmap_logic.jar;log4j.jar;ojdbc.jar" nn.gmap.logic.NNmain
And I get an error that the log4j cannot be initialized.
From the Eclipse environment the application runs fine.
Please let me know how should I execute the command properly.
Thanks.
Try to give the full path to the jars. I believe that there is a difference between what you think is your root folder and what Java thinks about it.
Something like java -cp "c:\myjars\nmap_logic.jar;c:\myjars\log4j.jar;c:\myjars\ojdbc.jar" nn.gmap.logic.NNmain
Btw, you can also do the following: java -cp "c:\myjars\*" nn.gmap.logic.NNmain
I am using Drools Planner which ships with 21 Jar files in a directory binaries. For example the
drools-core-5.3.0.Final.jar would provide org.drools.someClasses.
The included examples run it in command line by running an all-inclusive command:
mainClasspath=
for i in binaries/*.jar; do mainClasspath=${mainClasspath}:$i; done
mainClass=org.drools.planner.examples.app.DroolsPlannerExamplesApp
$JAVA_HOME/bin/java -Xms256m -Xmx512m -server -cp ${mainClasspath} ${mainClass} $*
I am developing the program in commandline. But the final application (which just adds a HTTP interface) runs in a JSP container, not from a commandline. Hence I need to programmatically import the jar files into my project.
Question:How do I programatically import the the jar files?
Do I rather have to specify the binaries path in the environment variable?
Update
I did this to tell Java that globally, extra classes may be found in the same directory, meant by the . and /home/jesvin/dev/drools/binaries. Note that : is the separator in Linux.
declare -x CLASSPATH='.:/home/jesvin/dev/drools/binaries'
You can do it per execution instance as per Miserable Variable's answer.
Finally, in a Tomcat deployment, I give it a HTTP interface, deploying it as per havexz's answer.
If the jars need to be in global scope:
Well if you want these jars to be available to all apps on the server try putting them in
`<tomcat_install_dir>/lib`
EDIT: #Geoffery comment: If you have access to
<tomcat_install_di>/conf/catalina.properties
then you can added your own common jars dir like:
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.base}/your_common_jar_folder/*.jar
(See carefully at the end, I had added a new folder)
But either way, if the jars meant to be at global scope they should be in any of above folders. Else you have to put other common jars for logging and jdbc etc with every app.
If the jars need to be in app scope
And if you want these jars to be used only one specific app then put them in
`<your_web_app>/WEB-INF/lib`
Note: You can put the <your_web_app>/WEB-INF/lib of your development folder and if you are using the right tools then it will make these jars part of your .war file.
SIDE NOTE: Since you are running java program from command line too and having issues adding dependency jars. Sometime back I wrote a shell script for this purpose, hope this will help you too.
Small snippet of it:
Run Java easily from shell command
Usage: easyrunjava [-c <jar_name/jar_dir>,<jar_name/jar_dir>...] [-m <email_id1>,<email_id2>...] [-p <prop_file1>,<prop_file2>...] class_name [args_to_program]
Example:
`easyrunjava -p .,./dependency -m user.name#xyz.com com.example.HelloWorld`
Are you trying to put all jars in a folder in classpath? import is the wrong word then.
Newer versions of java allow '*' as classpath:
$JAVA_HOME/bin/java -Xms256m -Xmx512m -server -cp 'binaries/*' ${mainClass} $*
should work. Note binaries/* is in single quotes to prevent shell expansion.
I have created a JAR file in this way jar cf jar-file input-files. Now, I'm trying to run it. Running it does not work (jre command is not found):
jre -cp app.jar MainClass
This does not work either:
java -jar main.jar
(Failed to load Main-Class manifest attribute from main.jar).
I also found out that
To run an application packaged as a
JAR file (version 1.2 -- requires
Main-Class manifest header)
What is the "Main-Class manifest header"? How do I create it and where do I put it?
I'm not sure I believe your symptoms:
If the jre command isn't found, then running jre -cp app.jar should give the same error
Just adding a JAR file to the classpath shouldn't give the error you're seeing
I'd expect you to see this error if you run:
java -jar app.jar
The Main-Class header needs to be in the manifest for the JAR file - this is metadata about things like other required libraries. See the Sun documentation for how to create an appropriate manifest. Basically you need to create a text file which includes a line like this:
Main-Class: MainClass
Then run
jar cfm app.jar manifest.txt *.class
set the classpath and compile
javac -classpath "C:\Program Files\Java\jdk1.6.0_updateVersion\tools.jar" yourApp.java
create manifest.txt
Main-Class: yourApp newline
create yourApp.jar
jar cvf0m yourApp.jar manifest.txt yourApp.class
run yourApp.jar
java -jar yourApp.jar
You can run with:
java -cp .;app.jar package.MainClass
It works for me if there is no manifest in the JAR file.
I got this error, and it was because I had the arguments in the wrong order:
CORRECT
java maui.main.Examples tagging -jar maui-1.0.jar
WRONG
java -jar maui-1.0.jar maui.main.Examples tagging
The easiest way to be sure that you have created the runnable JAR file correctly, with the appropriate manifest file, is to use Eclipse to build it for you. In your Eclipse project, you basically just select File/Export from the menu, and follow the prompts.
That way, you can be sure that your JAR file is correct and will know to look elsewhere if there is still an issue. The process is described in full in FAQ How do I create an executable JAR file for a stand-alone SWT program?.
I was getting the same error when i ran:
jar cvfm test.jar Test.class Manifest.txt
What resolved it was this:
jar cvfm test.jar Manifest.txt Test.class
My manifest has the entry point as given in oracle docs (make sure there is a new line character at the end of the file):
Main-Class: Test
Try
java -cp .:mail-1.4.1.jar JavaxMailHTML
no need to have manifest file.
I discovered that I was also having this error in NetBeans.
I hope the following is helpful.
Make sure that when you go to Project Configuration you set the main class you intend for running.
Do a Build or Clean Build
Place the jar file where you wish and try: java -jar "YourProject.jar" again at the command line.
This was the problem I was getting because I had other "test" programs I was using in NetBeans and I had to make sure the Main Class under the Run portion of the Project configuration was set correctly.
many blessings,
John P
I faced the same problem. This unix command is not able to find the main class. This is because the runtime and compile time JDK versions are different. Make the jar through eclipse after changing the java compiler version. The following link helped me.
http://crunchify.com/exception-in-thread-main-java-lang-unsupportedclassversionerror-comcrunchifymain-unsupported-major-minor-version-51-0/
Try running the jar created after this step and then execute it
If your class path is fully specified in manifest,
maybe you need the last version of java runtime environment.
My problem fixed when i reinstalled the jre 8.
If you using eclipse, try below:
1. Right click on the project -> select Export
2. Select Runnable Jar file in the select an export destination
3. Enter jar's name and Select "Package required ... " (second radio button) -> Finish
Hope this helps...!