Let's say I have a web-app.war containing Spring Boot application with libraries (.jar files) inside WEB-INF/lib. Then Someone has removed some of the libs from web-app.war. I put this archive on the deployment server and I have all the dependencies on that server (e.g. somewhere in /3rdparty/*.jar). I need to run my app with command java -jar web-app.war. I've read about classpath and I tried to do the following: java -cp /3rdparty/*.jar -jar web-app.war However I got the error: Could not find or load main class. I cannot rebuild my app within deployment server, I cannot avoid the situation that I described above, so is it possible to achieve my goal?
P.S. Probably this is a kind of dummy question but I really did not find anything non-trivial regarding this problem.
I think you can try use loader.path to add external jar. Also here some example
Related
I have a Java application with an embedded pentaho-kettle executor in it. basically it's a war (usually running from within jboss or tomcat) that executes KTRs using a java.
my setup is pretty much the same as in this post Pentaho Kettle within a web application
basically my question is this:
how can I tell which jar files I need in my classpath when executing a KTR?
or
how can I find the dependencies for a specific step?
for example, lets say I want to use the 'User Defined Java Expression' step, well, then through trial and error I now know that I have to have 'Janino.jar' in my classpath.
But what about other steps, how can I tell which Jars they require to execute?
There must be some better way, any suggestions?
Try Jboss Tattletale, a code analysis tool that produces among other things a report with missing classes.
This is an example of how to run it in standalone mode:
java -Xmx1024m -cp /c/dev/software/jboss-tattletale/tattletale-1.1.2.Final/javassist.jar -jar /c/dev/software/jboss-tattletale/tattletale-1.1.2.Final/tattletale.jar c:/temp/report
With this a report should be generated in c:/temp/report that includes among other things a page with missing classes. From there with findjar it's possible to find the missing jars.
i'm developing an application using Netbeans RCP. I have added an option to add a jar to my class path in the project.properties file of my platform:
run.args.extra=-cp:a ./appclient/glassfish/lib/gf-client.jar
The problem i encounter is that is does work when i run it from the Netbeans IDE but not when i try to create a independent application (build for Mac OSX for instance). I hear that the project.properties is no longer taken in account when you run an independent application and of course my appclient directory containing the jars does not exist anymore in the application package (so my jar is not added to class path).
How can i make this -cp option works for my independent Mac OSX application?
EDIT: i was able to create a custom conf file for my independent platform but i can't find a way to add my jar to the class path, i don't know what options to use.
EDIT: i found that i need to you endorsed mechanism to achieve it. So i have added the following command to my app.conf file:
J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar
But when i run the .app (mac application), i get this error:
-J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar: No such file or directory
The path is correct. Do i need to make something special to make the JVM aware of this? I followed this tutorial and somewhere in the comments the author says:
Right, but the package-appclient copies everything for you and you
should be able to put it on the classpath using the endorsed
mechanism. Unpack the jar created by that and add everything you need
from there (the jars) to your application installer. Then you can use
the endorsed (-J-Djava.endorsed.dirs=${GFCLIENT_PATH}) mechanism in
your app.conf to put it on the application classpath. This way you
should be able to deploy it together with your client.
I think, that create a new library is the better way.
Create module type library with required jars
In your module add dependency to created module (type wrapped library)
You must add entry Class-path to you application's MANIFEST.MF
For example
Class-Path: apache-commons-2.1.jar ejb-api-3.0.jar
all this jars should be in the root directory of your application
Your appliction should have next structure
MyApplication.jar
/META-INF
/META-INF/MANIFEST.MF
/apache-commons-2.1.jar
/ejb-api-3.0.jar
/com/package/classes
or you can use jar tool of JDK to create a jar
read more here Oracle doc
I am fairly new to java and just can't get my java application which relies of external libraries working...
I have two libraries and an application, all exported from eclipse as jars.
NOTE: I don't want them all exported into the same jar, as I wish to re-use the two libraries elsewhere.
enix.lib.common.jar (library 1)
enix.lib.events.jar (library 2)
enix.cmd.events.jar (console application)
When I run:
java -jar enix.cmd.events.jar
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: enix/lib/events/errors/EventLogNotAvailableException
Which obviously means it doesn't have the path to the enix.lib.events.jar (which if renamed to a zip file contains the file enix/lib/events/errors/EventLogNotAvailableException.class) - I then set the classpath in various ways like so:
java -cp ".;*.jar;enix.lib.events.jar" -jar enix.cmd.events.jar
But I get the same error. :(
I also have a GUI app called enix.gnome.events.jar which relies on various jars in /usr/share/java and /opt/libs/jars.
Could someone please explain what I am getting wrong and why, I would be most grateful! THANKS!
*.jar doesn't work, unfortunately. You can use wildcards in java classpaths, but only by putting all your jars in a directory and telling the classpath to use every jar that's there (see this).
I recommend, though, just listing out every jar that you need explicitly. Conventionally you'll see that most applications list every single jar.
When compiling/running java apps with multiple jars, I've found the easiest way is to just add the applicable jars right to my classpath. That way, when you compile or run the program, all of the applicable jars are available.
See http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html for setting the classpath in Windows/Unix/Linux.
When I finished to write my classes I put them into a package structure and then I jarred all.
Now which is the best way to deploy and use my jar?
setting classpath;
use CLASSPATH variable;
using the extension mechanism.
Don't update the user's CLASSPATH environment variable because there is a risk that your deployed application will interfere with other Java applications that the user might want to run.
Don't deploy using the Extension mechanism because there is a risk that you will interfere with applications run by any user using the JVM that you have "extended".
The best solution is to create and deploy a wrapper script that uses the "-cp" argument, or a shortcut that runs a self-launching JAR file ... as suggested by other answers.
I you have a main class in that jar that you want to run the best approach is to put a META-INF/MANIFEST.MF file in the jar and launch your class using java -jar mypackage.jar
The manifest should contain a Class-Path and a Main-Class attribute
See http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Manifest
If you mean by 'deploy and use' that you want to use it in another application, set it on the classpath of that application.
If you use the extension mechanism (I assume you mean putting the jar in pathtojava/lib/ext) every application using that jvm will have the jar loaded, the samecounts for the CLASSPATH as system variable. That is most likely not necessary?
If you ment to execute/use the jar as standalone application; just run it commandline, no classpath stuff needed. If you want to deploy on a server you probably wanted to make a war or ear fiel instead.
Best to my opinon is to provide batch files and shell scripts to start the application.
Sophisticated scripts check for environment variables like $JAVA_HOME (%JAVA_HOME%) and use them, if defined or use a default value.
Inside the script you could build the classpath in an internal variable and start the app with a line like
%JAVA_HOME%\bin\java.exe -cp %LIBRARIES% com.example.Main
I would prefer this solution over the java -jar Application.jar alternative, because this one requires that you setup the classpath inside the jars manifest. So deploying an application that depends on existing libraries on the target system is pretty difficults, just because you have to know the library paths before you build the application.
Setting the classpath (using -cp) is the best way, as you can have a different classpath for each application. The CLASSPATH environment variable is global for all Java applications running on the machine, as is the extension mechanism so you probably don't want to use those.
If your code is executable (ie it has a public static void main(String[] args) method) then you can also specify the class that the main method is in, and the associated classpath within the manifest file inside the Jar file. Once you have done that you can do this:
java -jar myJar.jar
And Java will work out the rest.
I am new to Java (and Eclipse) but I have used .NET (and Visual Studio) a fair amount. I also know about compiling C/C++ code and things like that. I know that at the end I get either an EXE or a nice binary file that can be run from the command line.
I have been making a Java utility that uses some external libraries. I need to compile this into an executable that I can run from the command line on a unix machine, but I cannot find any way to do this.
I can build and run/debug in Eclipse, but that is no use to me as this code will be run on a webserver. I just need all the dependancies compiled in to one file but after hours of searching on Google, the best thing I could find was the Fat-JAR plugin for Eclipse and after using that I just get the following error when I try to run the file:
Exception in thread "main" java.lang.NoClassDefFoundError: Network/jar
This is really confusing me and as it is such an essential thing to be able to do I am sure I must be missing something blindingly obvious, but as I said, after hours of searching I have gotten nowhere.
I really appreciate any help you can give me. Thanks.
If you build your java app using Maven (which can be done with every major IDE), then you can use the maven Shade Plugin to build a shaded jar file; this is a jar file with all of its dependencies included.
A shaded jar can be run from the command line like this:
java -jar myjar.jar command line options
You're doing something standard and you're using eclipse. This means, in your case, Maven is your friend. Download and install the M2Eclipse plug-in. Maven is best at managing dependencies. So, creating a jar with dependencies included will be very, very straight forward. There are thousands of examples on the web and in StackOverflow. If you have problems setting it up, comment on this and I can point you in the right direction.
Sounds like your class path on the server needs to be modified to pick up the jar file containing the Network class. How are you executing your program? What path(s) are you putting in the -cp option?
If you are not sure how to find out the contents inside a jar file, run jar tf , this will list the packaged classes. Validate that one of the jars in your CLASSPATH has that class it says missing.
Give us more details and we can help solve it.
I think I should first explain some basics. A Java class can be run as an application if it has a public static void main(String[] args) method. If it has this method, you can run it from command line as:
java my.package.MyClass <attributes>
Before launching your app, you need to make sure that all required .jar files (and the root of your own class folders, if you did not make a jar from your classes) are in the CLASSPATH environment variable.
These are the absolute basics. When you are building a more complex app, or an app for distribution, you'll probably use maven or ant or some other tool that makes your life easier.