I have a requirement to add some custom classes and JSPs to an existing java web application. The custom classes must be in their own package, away from the existing java classes. The existing application is built on the struts framework.
I'm fairly new to all this and need guidance on the following:
Using Eclipse IDE, If I import the existing WAR file and libraries and unpack the WAR file, how do I compile new .java source files and add the .class files to the WAR?
The existing .class files are contained in a JAR. Do I need to create a JAR file containing the new .class files outside of eclipse and then add it to the same location as the existing .class JAR file i.e. WEB-INF/lib (there is no WEB-INF/lib/classes folder)
Adding the JSPs and amending the web.xml file should be fine but are there any pitfalls to look out for ?
Any and all help greatly appreciated
If I import the existing WAR file
I don't advise you to import third party war file into Eclipse. Instead do the following:
Create a dynamic web application in Eclipse (See the screenshot below):
Implement your custom classes and put them under the folder Java Resources / src
Put the JSPs under WebContent / jsps (you can choose any name you like)
Create a new directory in your file system (Windows / Unix ...) and copy the thirdparty war into it.
Unpack the war file using command line as:
jar xvf mythirdparty.war
Copy your class files (they should be under build / classes folder; see screenshot) to WEB-INF / classes folder of the unpacked thirdparty war.
Copy the JSPs (folder jsps) to the root of the unpacked war.
Remove the the old war (of the thirdparty war) so that
Navigate to the root and pack again as:
jar cvf thirdparty.war *
Related
I have created a JavaFX application using NetBeans IDE and below is my folder structure.
I want to a build a single jar file including all dependencies for this jar to work properly.
This jar requires testplanner and batch folder from project root directory and files inside dist folder to work properly.
How can I package all this to a single jar file?
Theoretically JAR files cannot contain dependencies within, as java does not support it out of the box. WAR and EAR archives can. What You want to do is not standard, but is named fat jar. Fat jars are used i.e. by spring-boot maven plugin, but you could try this:
https://dzone.com/articles/how-build-fat-jar-using
And some more explanation:
NetBeans - deploying all in one jar
Use tecreations Deploy. Put all your sources into a path declared as Tecreations.getProjectPath(), run BuildAll to create your corresponding classes, put your jars in projectpath/jars and select the appropriate settings, whether to include jars, sources or classes. Select your main class and click Deploy. Unsigned and signed output jars are produced in user/Documents.
Download: https://tecreations.ca/java/downloads/release.
I am trying to learn about the Spring framework for Java and I am not sure where about's I am supposed to put the applicationContext.xml file for configuring the spring beans. My directory structure is as follows:
Do I put in .settings? Or should it be put at the top level within springapp?
Thanks for the help.
Put into directory WebContent/WEB-INF.
Resources placed into WEB-INF folder are not accessible from web, they are application internal resources. This is good, because your applicationContext.xml shouldn't be accessible from web.
Other good options are WebContent/WEB-INF/classes or just src (both are equal).
Files and folders with . contains Eclipse configuration files, they are internal for Eclipse - do not use them.
I recommend to put it in the src (or src/META-INF) folder and access it via classpath:applicationContext.xml (or classpath:META-INF/applicationContext.xml). (Eclipse will copy this file to WebContent/WEB-INF/classes when it build the war archive.)
Because:
The mayor advantage of src over src/main/webapp/WEB-INF / WebContent-WEB-INF is, that you can access the src files even in the tests (via classpath:applicationContext.xml)
Do NOT put it .settings because the content of this directory gets not deployed in the Web App (it is eclipse configuration folder)
Of course when you use maven, then put the file in src\main\resources (or src\main\resources\META-INF), Maven will copy them to the classpath folder while compiling.
WEB-INF or its subdirectories. This folder's content is packed directly into the root war file, so files that are directly under this folder are accessible as resources with path like '/foo.xml' (or in spring notation classpath:/foo.xml
It needs to be in the classpath. You can put the original editable instance anywhere (e.g. a config directory off the root) but then you will need to have your build management tool (e.g. Ant or Maven) copy it into the classpath for the runtime.
My project structure is the following. Server is Glassfish 4. IDE - netbeans 7.4. Project temp is used by project a.
EAR
+ lib/temp.jar
+ a.war
Temp.jar contains core classes. They are used by war. Temp.jar doesn't import any files from war, and it must know about war nothing. But, temp.jar has classes that creates instances of war classes by reflection (according to xml file). Here I get ClassNotFoundException. How can it be fixed? Is it possible to fix or I have wrong project structure?
Note: I didn't put temp.jar in a.war as there can be different war files that can use this jar.
I found the answer.
If we put jar into ear/lib folder then the classes in it can be accessed but they can't access the classes out of lib (in this case there is no need to add something to manifest file of war).
If we put jar in root of ear or in any other folder ear/bum than the classes of these jar can access war classes.
So I conclude, that /lib is monodirectional, any other are bidirectional.
Hope it will save someone a lot of time.
Add jar file to the you lib folder.
Change MANIFEST.MF and add the particular jar path to Bundle-ClassPath.
That worked for me.
I use the jstl library in my project, but as an external jar. After exporting my project to a .war file, these libraries are not included and I'm getting errors. How do I make sure the library is included in my war file?
Also, an unrelated question, if an ear file would have only one war module, is there a point to making it an .ear instead of .war
If you have put the jstl jars inside WebContent directory they will be packed in your WAR file. But if you are loading them from somewhere else make sure that you have them in your export list (Project Properties -> Java Build Path -> Order and Export).
If you have an EAR file you can make some extra settings in your application.xml file, or even some container specific ones.
i have created a java application which uses data from its config folder and , it also uses third party jar files those are located in lib folder, could anyone tell me how to create jar file for this project with the content stored in config file and lib folder.
i tried creating jar using eclipse export functionality. when i run this jar file, it says it can not find the third party libraries that i have used for this project and configuration file.
thanks in advance for any help
You can create a Runnable JAR in Eclipse 3.4+ in the Export wizard selection dialog (right click on a project and go to Export...) using an existing launch configuration which will incorporate the libraries or repack them. Config files should be readable from the same directory as the runnable jar is located. If you need any help with loading these in, just ask :)
(source: eclipse.org)
You have two options
include the stuff in the third-party jars in your jar
provide access to the jars on the classpath when you run your jar.
Both have their benefits and their drawbacks.
Java does not support putting JAR files inside executable JAR files, so you can't just put your third-party library JAR files inside your own JAR - Java won't be able to find them.
If you don't want to distribute your application as a whole bunch of JAR files, you can use a look such as One-JAR which will build a JAR file for you that contains your own classes plus the classes of the third-party libraries that you're using.
To learn more about how to package a program in an executable JAR file, see Packaging Programs in JAR Files in Sun's Java Tutorials.
If you use netbeans just by click on "build" a jar file will show up in the "dist" file in your project directory