Create a war file including external jars using command prompt - java

I have two projects - Project1 and Project2. Project1 contains all the java classes. And project2 contains all the jar files required in project1. Means, Project1 depends on Project1 (for jars).
I am using Glassfish server. While creating the war file of Project1, I included all the jar files from project2.
Example:
Project2 conatins 2 packages - POI and web-services.
POI package contains: poi-examples-3.6-20091214.jar,poi-contrib-3.6-20091214.jar, poi-ooxml-3.6-20091214.jar, poi-ooxml-schemas-3.6-20091214.jar, poi-scratchpad-3.6-20091214.jar.
web-services package contains: javax.servlet-3.0.jar, javax.servlet-api.jar, jersey-bundle-1.10.jar, jersey-client-1.12.jar, jersey-core-1.12.jar, jersey-multipart-1.4.jar.
Now for creating the war file for Project1 from command prompt, I used the following command:
jar cvf Project1.war D:\JavaProject\Project2\lib\external\poi\poi-examples-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-contrib-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-ooxml-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-ooxml-schemas-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-scratchpad-3.6-20091214.jar D:\JavaProject\Project2\lib\external\web-services\javax.servlet-3.0.jar D:\JavaProject\Project2\lib\external\web-services\javax.servlet-api.jar D:\JavaProject\Project2\lib\external\web-services\jersey-bundle-1.10.jar D:\JavaProject\Project2\lib\external\web-services\jersey-client-1.12.jar D:\JavaProject\Project2\lib\external\web-services\jersey-core-1.12.jar D:\JavaProject\Project2\lib\external\web-services\jersey-multipart-1.4.jar
Project1.war file created in the current directory.
But, when I tried to deploy the Project1.war on Glassfish server, I encountered this error:
C:\glassfish3\glassfish\domains\domain1\applications\Project1\D:\JavaProject\Project2\lib\external\poi\poi-contrib-3.6-20091214.jar
(C:\glassfish3\glassfish\domains\domain1\applications\ is the directory where '.war' files are copied after deployment)
I could not understand the problem. Is something wrong with the command, I executed for creating war file? How are external jars included while creating war file using command line prompt?
Please help.. Thanks in advance!

A WAR file has to have a specific file structure.
/
META-INF/
WEB-INF/
classes/
classes and other classpath resources go here
lib/
jar files go here
web.xml
other webapp stuff; e.g. HTML, JSPs, CSS ...
If you just throw stuff into a JAR file at random, it won't be recognizable as a webapp when it is unpacked by the webapp container.

You should be using the WAR exploded layout, in other words, project2 should be included at $PROJECT1_DEPLOYMENT_DIR/WEB-INF/lib and so on. Then your jar command will result in a deployable file to Glassfish or any other servlet container.

Related

Put Jar file into a folder and package as zip

Oracle Identity Manager application needs plugins to be deployed as I explained below:
There must be a zip file contains 2 folders and plugin.xml
folder structure is looks like this:
lib folder must contain project itself packaged as jar, META-INF folder must contain pluginPostProcessEventhandler.xml
This folders and file structure needs to be zipped as pluginPostProcessEventhandler.zip to be deployed. Using Jenkins. is there any way creating this folder and file structure as output folder by using pom.xml?
Thanks
With the Maven assembly plugin, it should be possible to create a zip in the way you describe.

Keep resource file on root level in repackaged JAR file

I'm using the Spring Boot Maven Plugin in my pom.xml and more precisely the repackage goal to create an executable jar: https://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/maven-plugin/examples/repackage-classifier.html
This moves all application classes under BOOT-INF/classes in the JAR file.
In my case I have a file under my src/main/resources path, let's say it's src/main/resources/my-file.txt.
In a normal JAR file the above my-file.txt would be placed at root level in the JAR.
When I repackage the JAR however, the file goes to BOOT-INF/classes/my-file.txt. This is a problem for me as I really need this file on root level in the JAR.
Is there a way to achieve what I'm looking for with maven somehow as I don't want to have to manually insert the file on root level into the JAR with a command afterwards?
Thanks!

Creating war from its component folders

I have deployed my war file on a remote linux server. I run this war using jetty-runner. Its not feasible for me to push this war multiple time. Its size is huge and it takes aprrox 45 min to push a fresh war onto the server. To handle this issue I thought of using the following steps(with commands) :
unzip:Unzip war to its corresponding files/folders : WEB-INF, META-INF, index.jsp.
Updating new class file in WEB-INF.
zip:Repacking these folder into a war again.
But the newly created war does not work. Is there a standard/correct way to pack these files into a war. Also, jar command is not available on the server.
Please suggest.
P.S. Already looked into various SO questions but didn't find any useful solution.
The zip command does not work as expected. The war packed by that command did not work. Instead, we have to use the JAR command.
I was able to generate the war after modifying the contents by using :
jar -cvf webproject.war index.jsp META-INF/ WEB-INF/
Note: If the jar command is not available on the server, specify JAR path using installed java on the server:
PATH_TO_JAVA/bin/jar -cvf webproject.war index.jsp META-INF/ WEB-INF/

ClassNotFoundException when using a self-created jar file

Using Eclipse I created some parser classes I want to provide to another project as a jar archive for validation purposes. So the parser project look like this:
ParserProject
- src
-- com.package.x
--- ClassA
--- ClassB
- lib
-- external1.jar
-- external2.jar
The ClassA and ClassB use the external jar archives, like Jackson or some Apache commons. To provide the functionality to another project, I exported the entire project as jar archive and executable jar archive (Right click on project > Export... > Java > JAR file > Select all files and "Export generated class files and resources" > Finish).
The jar file is created without any errors. When I use the parserproject.jar in my validation project, I can access all my methods using auto completion, but when I run the validation project, I get a java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException.
Now three strange things:
All jackson jars are included in the parser project. Besides, I can run a main() method in the parser project and everything works fine, no ClassNotFoundException occurs.
When I add the parserproject.jar to my validation project in the class path and open the jar archive in the Package Explorer, the parserproject.jar seems to contain all jars it needs.
For the executable jar archive, all required external jars are contained in the MANIFEST.MF (Package Explorer > validation project > Referenced Libraries > + besides parserproject.jar > META-INF > MANIFEST.MF). It looks like this:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ json-20140107.jar jackson-annotations-2.5.4.jar ja
ckson-core-2.5.4.jar jackson-databind-2.5.4.jar commons-io-2.4.jar co
mmons-validator-1.3.1.jar slf4j-api-1.7.5.jar slf4j-log4j12-1.7.5.jar
json-schema-validator-2.2.6.jar jackson-module-jsonSchema-2.4.4.jar
juniversalchardet-1.0.3.jar snakeyaml-1.15.jar commons-beanutils-1.7.
0.jar commons-digester-1.6.jar commons-logging-1.0.4.jar joda-time-2.
8.1.jar jopt-simple-4.6.jar jsr305-3.0.0.jar json-schema-core-1.2.5.j
ar libphonenumber-6.2.jar jackson-coreutils-1.8.jar commons-lang-2.6.
jar guava-16.0.1.jar msg-simple-1.1.jar btf-1.2.jar mailapi-1.4.3.jar
uri-template-0.9.jar
Class-Path: .
Rsrc-Main-Class: com.package.SchemeValidator
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
I get the exception if and only if I use the generated jar file in my validation project. In case I get rid of the parserproject.jar and define a dependency to the ecplise parser project instead (Right click on validation project > Properties > Java Build Path > Projects) I do not get the ClassNotFoundException.
So now my question is, how I should export the jar so that every class is found. Thank you!
Eclipse only takes care of the compile-time dependencies while generating a .jar
Since your generated .jar can be moved to virtually anywhere, the dependencies must again be present during execution time.
You have two options:
Execute your jar with the -jar option, while leaving all
dependencies in the same folder. Since your manifest uses "./" as classpath, this means all dependencies must be on the same directory you are executing your jar from. NOTE classpath is relative to the directory you are executing from, not the directory the file is on.
Execute your jar withour the -jar option, and specify the -cp option to point to the dependencies, and the specify the main class.
java -cp "<path to your jar>;<path to dependency 1>;<path to dependency 3>[;...]" <your main class>
You might consider creating a so called fat jar which will contain all the needed classes. For example: http://fjep.sourceforge.net/
If you do not want to go through the hassle of managing all the depencencies by yourself, consider using a build tool like
Maven https://maven.apache.org/ or Gradle https://gradle.org/.

Making a JAR file which includes all files of a cetain type from any subdirectory

So this is a simplified version of my package structure
Project 1
-folder1
-folder2
-folder21
-folder211
-test3.java
-folder22
-folder3
-test4.java
-Project2
-folder1
-folder11
-folder111
-Test.java
-folder2
-.properties
-Test2.java
-folder3
What I want to find is command that will create a jar and take the paths to project1 and project2 and recursively add the folder structure and java files without adding the .properties files.
What i've tried so far is
jar cvf test.jar "pathtoproject1/.java" "pathtoproject2/.java"
That only works for java files in the base project directories not the subfolders.
Anyone know how to do this?
edit
This is for a batch script on windows
Frankly I'm not sure that jar handles this out of the box.
I suggest using ant - with an ant jar task, using a fileset.

Categories