how to run a spring boot application from linux using putty - java

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.

Related

How to add additional jar file to spring-boot application at runtime?

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"
}

How to let an automatic module find its own resources in Java 9?

I am trying to get my application to run with Java 9, but unfortunately one of the plain jar dependencies, when it tries to load a resource using classLoader.getResource(name), gets a null instead.
This, of course, works in Java 8.
I declared a dependency on the module in question using the module file, referring to the name of the module by its jar name (awful), and added the jar as-is (no customization for Java 9) using the --module-path option.
Here is my approximate module file declaration:
module my.mod {
requires ivy; // the file is called ivy-2.4.0.jar
}
And I run with this command:
java --module-path my-mod.jar:ivy-2.4.0.jar -m my.mod
When I run the application, it works fine if the library doesn't try to load that resource... but if it does, it gets a NullPointerException at the line it tries to use the resource.
I can see the resource is present in the correct path in the jar file.
I've tried running my application both as a jar (shown above) and just with the class files:
java --module-path modules-dir:ivy-2.4.0.jar -m my.module/my.Main
In the latter case, the error is different, but related: Ivy can't even find a properties file it tries to load from its own jar!
Is there any workaround, is this a known bug, or am I doing something wrong?
Try to add an 'opens' declaration to the automatic module 'ivy'. That allows access to it's resources:
java --add-opens ivy/<dot.separated.path.to.resources>=ALL-UNNAMED --module-path my-mod.jar:ivy-2.4.0.jar -m my.mod

How to specify a runnable jar to use tomcat's lib for dependencies

I have made a java project in eclipse that's a runnable jar if I export it.
My structure of my project :
When I export my project as a runnable jar it asks me how I would like my dependencies to be packaged and I choose to put it in a lib folder. So when exporting it exports the runnable jar as well as creates a sub folder with my dependent jar files which I want to be only the 5 jars located in Referenced libraries.
This is the export window :
This creates my runnable jar and then a folder _lib with my other jars.
This works fine but as you see in the first image I also included the Tomcat lib library because some of the jar files in tomcat lib are used by my project.
The problem is that when I export, all the jar files in the tomcat lib get exported to my sub lib folder which I do not want.
Basically I want to deploy my runnable to a server witch will get the dependent jar files from the sub directory lib only those 5 jar files in image (Referenced Libraries). Tomcat is installed on the server so it should point to tomcats lib to get the rest of the jars.
Want my project to use the generated lib folder and then the tomcat lib.
Hope it makes sense what I am trying to ask.
Using Eclipse and java 1.7. developing on windows, deploying to linux running the application via command line on linux box.
What I have done to get this working is on the linux side to start the java runable class another way by specifying the classpath this will give the class you are trying to run the ability to first check the folders that you specify. So in my example instead of executing my app on the linux box with the following :
nohup java -jar /usr/local/P6/StockListShunter/StockListShunter.jar &
I did the following and specified the Class path lib that java should check for .jar file dependencies :
nohup java -classpath :/usr/mylib1/*:/usr/StockListShunter_lib/*:/usr/share/lib/*: stockListShunter.Shunter &
Notice I specified 3 libraries java should check when running my runable class, mylib1,StockListShunter_lib and lib.
After you specified the classpath you will see you need to specify your java class witch will be your main method class. In my example it is stockListShunter.Shunter where stockListShunter is my package name.
Just take note that you should make sure that the class you are trying to excecute is included in one of the jar files that you specified in the classpath otherwise it wont find the class and you will get an error : Error: Could not find or load main class stockListShunter.Shunter
So when you run the command the process will be started, my program is in a loop so the process will be alive until I kill it. If your program should be alive you can confirm it is running by running the following command.
ps aux | grep stockListShunter.Shunter
Where stockListShunter.Shunter is your main class name. This should display all the processes where the command has a stockListShunter.Shunter in it.
If you want to end the process like mine that's in a loop you can run the following command.
pkill -f 'java.*stockListShunter.Shunter'
Again where stockListShunter.Shunter is your main class name. This should kill your java process.
Hope this could help some one in the future.

Setting classpath for Oracle JDBC jar in centos

I am fairly new to Linux, I am trying to set my jar files for OJDBC in my classpath but so far have not found any good examples for a beginner such as me.
I will list the my paths for Jar file below. Can someone provide me with example or how to step by step set my class path with the commands. I would really appreciate it. I would like to set this path in my project which I am using it in.
Jar file:
/home/ssingh/Downloads/oracle.jar
Project:
/opt/savi/Workspace/SgRecc/bin/PerVal.class
Setting up the Class Path
To use the driver, the JAR archive named postgresql.jar if you built from source, otherwise it will likely be (named with the following convention: postgresql-[server version].*[build number].jdbc[JDBC version]*.jar, for example postgresql-8.0-310.jdbc3.jar) needs to be included in the class path, either by putting it in the CLASSPATH environment variable, or by using flags on the java command line.
For instance, assume we have an application that uses the JDBC driver to access a database, and that application is installed as /usr/local/lib/myapp.jar. The PostgreSQLâ„¢ JDBC driver installed as /usr/local/pgsql/share/java/postgresql.jar. To run the application, we would use:
export CLASSPATH=/usr/local/lib/myapp.jar:/usr/local/pgsql/share/java/postgresql.jar:.
java MyApp

Getting ClassNotFound Exception when executing jar

Am using IntelliJ 12.0.4 Community Edition.
I created a Java console app called DB with main class name of DB.
I packaged it into an executable jar file called DB.jar.
In that app I connect to an Oracle DB using JDBC.
I packaged the necessary JDBC jar files into the DB.jar via the Intellij's Project Structure (Modules, Library)
When I execute the app from within IntelliJ it runs successfully
If I copy DB.jar to some directory and execute it via "java -jar DB.jar" I get an ClassNotFound exception on oracle.jdbc.driver.OracleDriver
I looked in DB.jar and the jdbc jar files (ojdbc6.jar, ojdbc14.jar) are in DB.jar
Any thoughts?
Jars in an executable jar are not on the classpath, typically.
You can put them in the same folder as DB.jar and do:
java -cp DB.jar;ojdbc6.jar;ojdbc14.jar <MainClass goes here>
and that should run it.
You could also put the Class-Path entry in the Manifest.MF file inside your jar to reference other jars relative to the location of DB.jar in the computer's file system. Reference the two other jar files and then you can do java -jar DB.jar <MainClass goes here> (assuming the two jars are in the right place.
There is some discussion here about the general frustration about not being able to do what you want to do (and what many others have wanted for years).
Classpath including JAR within a JAR
Explain about Main Class
When you run a java application from the command line, there is some class where the execution starts. It will look something like this:
package com.mycompany.app;
public class StartHere {
public static void main(String[] args) {
// Your code goes here ...
}
}
If this example was the class where your application starts you would use a command line this to start it. This assumes you have the StartHere class in the right package location in a jar on the classpath:
java -cp DB.jar;ojdbc6.jar;ojdbc14.jar com.mycompany.app.StartHere
Notice that this allows you to have multiple applications in the same jar. Just make multiple classes with a main() method and run them with different starting classes shown on the command line.

Categories