There are some ways to load external jar libaries such as using loader.path for spring boot application and -classpath for many other java applications including tomcat.
The question is whether it is possible to load an external libaries with some dependencies in it together for a java application (spring boot, tomcat and so on). The external libary may have the following structure:
xxx.jar
│
└───<compiled class file folder>
│
└───libs
│ d1.jar
│ d2.jar
| ...
It looks like you are trying get external dependencies from a "fat jar". A fat jar is a single, executable jar file that contains all the necessary dependencies and resources needed to run the application. In other words it doesn't indented to be loaded as a library. It is ready-to-run application (you even don't need to add other classes using -classpath).
Technically, of course, you can extract all jars from xxx.jar and then load them using the same -classpath and loader.path options, but I believe it's a wrong way at all (and if you still want to do so, you can read how to unarchive jar here).
But I highly recommend you to make xxx.jar as a simple dependency (without /lib folder) and then download d1 and d2 dependencies directly from some central repository or add them as a system library using system path. Example for Maven:
<dependency>
<groupId>com.tonny.xxx</groupId>
<artifactId>d1</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/d1.jar</systemPath>
</dependency>
You can read how to add dependencies from filesystem here.
Related
How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?
You have 2 options here:
extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
link the dependent jars via the Manifest.MF and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into out\single and out\linked directories.
Relevant configurations:
If you are using maven to build your application then this is not the correct way to add external library. You should either
Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
Use system path like explained here.
Option 1 is prefered since you don't have to keep jar in your project.
How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?
You have 2 options here:
extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
link the dependent jars via the Manifest.MF and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into out\single and out\linked directories.
Relevant configurations:
If you are using maven to build your application then this is not the correct way to add external library. You should either
Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
Use system path like explained here.
Option 1 is prefered since you don't have to keep jar in your project.
My directory sturcture is,
C:\Grapher\src\ *.java
C:\Grapher\lib\ *.jar
There are many java files and many jar dependencies. I want to create a tomcat deployable WAR.
Can any one suggest me how to create a WAR using a gradle script? What are the dependencies to be included in Gradle to create a WAR.
Have you read and understood https://docs.gradle.org/current/userguide/war_plugin.html?
The War plugin adds two dependency configurations named providedCompile and providedRuntime. Those two configurations have the same scope as the respective compile and runtime configurations, except that they are not added to the WAR archive. It is important to note that those provided configurations work transitively. Let's say you add commons-httpclient:commons-httpclient:3.0 to any of the provided configurations. This dependency has a dependency on commons-codec. Because this is a “provided” configuration, this means that neither of these dependencies will be added to your WAR, even if the commons-codec library is an explicit dependency of your compile configuration. If you don't want this transitive behavior, simply declare your provided dependencies like commons-httpclient:commons-httpclient:3.0#jar.
If you use providedCompile or providedRuntime, those dependencies will not be added to your war archive.
The folder structure you need to have in your project is:
app
|-src
|-main
|-java
|-webapp
In the java folder you add your own packages and classes. In the webapp folder you add web content if your application makes use of such items.
I'm having trouble deploying a JAR I created from a groovy script, and I think it's because in the script I'm using Grape's Grab functionality to pull in a few libraries, and then when I build the JAR those libraries aren't included in the class path or anything.
How can I go about converting these #Grab statements to "import" statements?
Grape should work if you include all groovy libraries into your application.
OR
use gradle to build your library with dependencies, put dependencies (grabs) into build.gradle and exclude them from your groovy. in this case you need to put into your application groovy-all-XXX.jar with your library and other graped dependencies.
FYI
you can collect all dependencies in a strange way like this:
by default artifacts stored in ~/.groovy/grapes directory
for windows it's %USERPROFILE%\.groovy\grapes
see here how to customize it
the simple way to take all jars:
to specify custom empty directory for cache
and after Grape command list all jars in this directory and include them into deploy with your library.
but this like creating custom gradle )
I want to build a runnable jar for a JavaFX application which uses depency injection (Spring framework libraries) to manage multiple FXMl files and multiple FXMLDocumentControllers.
However, when exeuting the jar a "ClassNotFoundException: org.springframework.context.annotation.AnnotationConfigApplicationContext" is reported.
Yet, I have added Spring library (3.2.7) to the project in Netbeans and it functions well within the IDE.
How can I assure, that the above class is available in the jar as well?
When compiling and packaging your code, the resulting jar usually only contains YOUR code. You need to add the dependent jar files to the classpath. What that means is, whenever you invoke java -jar yourapp.jar some.package.MainClass to run your application, you need to list all required jar files in the -cp option of the java command.
Another (bad) solution could be to use maven's assembly plugin (or some other means) to create a fat-jar, which essentially is a merge of yourapp.jar and all dependent jars.
Read: Building a fat jar using maven