create jar for webapp - java

i'm using netbeans for writing a webapp + filter.
I want to generate a jar file (to place in tomcat/lib folder) for the sake of the filter.
when i compile the project, it generates war file.
is there a way to tell netbeans to generate a jar file instead?

You can add something like this to your build.xml:
<target name="-post-compile">
<jar destfile="${basedir}/dist/my_web_app.jar">
<fileset dir="${basedir}/build/web/WEB-INF/classes">
</fileset>
</jar>
</target>
Which will build you a jar alongside your war.
EDIT: See Ravindra's answer below for a slight improvement to this method.

An enhancement to #DeadPassive answer - You can get the web app name using the variable ${ant.project.name}
<target name="-post-compile">
<jar destfile="${basedir}/dist/${ant.project.name}.jar">
<fileset dir="${basedir}/build/web/WEB-INF/classes">
</fileset>
</jar>
</target>

Create a separate project to generate your .jar file. In that case also I think you have to create the .jar using terminal. You can't create a .jar file for a web project using netbeans IDE, as far as I know.

Maybe modify the build.xml used by netbeans, adding a copy task on the generated jar.

Related

Netbeans generated jar file not working

I have the initial code generated by netbeans in the new JFrame Form which shows the following output when run:-
But, when I run the jar file in the dist folder, I get the following:-
How, to solve this problem? Any help will be appreciated.
You will need to edit your build.xml so that all your used libraries are included in the build process.
This will look something like this in build.xml:
<target name="-post-jar">
<echo>Adding jar: ${reference.myMissingJar.jar}</echo>
<jar update="true" destfile="${dist.jar}">
<zipfileset src="${reference.myMissingJar.jar}"/>
</jar>
<mkdir dir="bin"/>
<copy file="${dist.jar}" todir="bin" description="Copy jar file."/>
</target>
The problem is that while you try to run it in netbeans, the IDE knows about all those nifty libs you use. When using a single jar, that is something different altogether.
Alternativly you might want to state the locations of your used libs in your classpath parameter.
In netbeans do a Clean and build project (shift + f11)
if that doesnt help please post more details.
Are u using external libs?

Add resources to a JAR

I want to include resources fils (images, textfiles...) in a JAR.
I added my asset folder to the classpath, so my program works fine when I run it in netBeans.
But if I build the project, I receive an error :
Not copying library C:\Users\Flow\Desktop\cp , it's a directory.
Is it a normal error ?
So I tried to add assets manualy like that :
<target name="-post-jar">
<jar destfile="dist/MonProjet.jar" update="true">
<fileset dir="C:/Users/Flow/Desktop/cp">
<include name="assets/*"/>
</fileset>
</jar>
</target>
But it does not work. Do I something wrong ?
Sorry for my english and thank you for help.
Don't place the files on your desktop. Go to the Source folder of your project and copy it in that folder. In NetBeans, any resources you place in the src tree will be automatically copied over to the classes tree when you do a Build (or Clean and Build), except those files matching the "Exclude From JAR File" skeletons specified in Project -> Properties -> Build -> Packaging. So if you pasted it there it should be in your jar file.
Your directory structure should now look like this:
Project0
-build
--classes
---asset
---project0
--empty
-nbproject
--private
-src
--asset
--project0
-dist
-test
Source: http://www.coderanch.com/t/511437/java/java/Adding-images-Jar-Files-NetBeans

Runnable JAR file export

I have a java project . I want to export it to runnable jar file.
I use eclipse to do it.
But when i run created jar file i receive Error ~ file not found : config\file.xml (the system cannot find the path specified).
How can I export a folder to run success jar file at any where ?
If you don't need your config to be configured outside of jar, just include it in there and access with something like getClass().getClassLoader().getResourceAsStream("config.xml") if it's located at the level of "root" package in jar
Sounds like your application is looking for a configuration file using a relative path in the file system. In order to make the jar totally self-sufficient, the code would have to be modified to look for the config file in the classpath, and the file would have to be included in the jar.
To do this, the code that opens the file must be changed to use Class#getResourceAsStream() instead of using a File object.
I'd suggest you use ant or another build system. Here is a short tutorial on ant:
http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
Its pretty easy to create an jar file using xml in your ant build script. I have done it many times:
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
Others may suggest maven and the like, but in reality they are all good choices. Exporting from an IDE such as eclipse really isn't a viable long term solution.
if you have a config-folder in your projects directory, and there is a file you want to read from, you have to copy this folder to the directory of your jar-file too

Export as runnable jar, but with editable property file?

I have a property file for a client project. I want to export the client as runnable jar, but the properties file should not be integrated into this jar. Because I want of course make edits to it, without having to recompile the jar client file.
How can I export it this way with eclipse?
I'm not currently using Java, but i once used Eclipse to export a runnable JAR. I used the Properties to store the settings and the file was generated automatically in the surrounding folder of the Jar-File. I don't know if this helps you, but it might be a point to start with....
You have to either put them in your project workspace so it is inside the jar (but this is not the case since you do not want that) or load them from a predefined directory, for example you could demand properties file to always be in the same directory as the runnable jar file.
By the way, you don't have to recompile your project if you put your properties file inside a jar. A jar file is just a package, you can unpack it, make edits to your properties file and then pack it again. It will work just as you demand.
If you change your mind, see this post.
If you want the properties file in the same directory as your JAR file, just add an extra step to your Ant build to zip up the properties file and the JAR file.
If not, have your Java code create the properties file where you want it the first time the Java application is run.
It does not seem to be possible to do this using standard Eclipse's export as runnable JAR (right click project -> Export -> Runnable JAR file -> Save as ANT script). However, Eclipse allows you to store the definition of your export as an Ant file, that one can modify later on.
The modification can look like below (I marked the modified parts with comments; it should be easy to compare with a script that your Eclipse generated).
<target name="create_run_jar">
<jar destfile="/mytargetlib/myjar.jar">
<manifest>
<attribute name="Main-Class" value="Main"/>
<!-- Here we only add lib/properties, the rest should be pre-generated by Eclipse -->
<attribute name="Class-Path" value=". lib/myjar1.jar lib/myjar2.jar lib/properties"/>
</manifest>
<fileset dir="/bin" excludes="**/*.properties" />
</jar>
<delete dir="/mytargetdir/lib"/>
<mkdir dir="/mytargetdir/lib"/>
<copy file="myjar.jar" todir="/mytargetdir/lib"/>
<copy file="/myprojectdir/lib/myjar1.jar" todir="/mytargetdir/lib"/>
<copy file="/myprojectdir/lib/myjar2.jar" todir="/mytargetdir/lib"/>
<!-- Again, all the above was generated by Eclipse. We only add a copy of our property file -->
<mkdir dir="/mytargetdir/lib/properties"/>
<copy file="/myprojectdir/properties/mypropertyfile.properties" todir="/mytargetdir/lib/properties"/>
<!-- end of our addition -->
</target>

How can I JAR up my classes before uploading to Google App Engine

I have read that JARing up Java classes before uploading them to App Engine improves performance. Can anyone explain how that is done? Can that be achieved from within Eclipse?
You can easily use Ant to make the jar
<property name="staging.dir" value="war"/>
<property name="classes.dir" value="${staging.dir}/WEB-INF/classes"/>
<target name="jarClasses">
<delete file="${staging.dir}/WEB-INF/lib/classes.jar" />
<echo> ${staging.dir}/WEB-INF/lib/classes.jar DELETED </echo>
<jar destfile="${staging.dir}/WEB-INF/lib/classes.jar" basedir="${classes.dir}" />
<echo> ${staging.dir}/WEB-INF/lib/classes.jar JARRED </echo>
<delete dir="${classes.dir}" />
<echo> ${classes.dir} DELETED </echo>
</target>
My layout is like:
/war
/build.xml
Alternatively you can use an Ant task to deploy
Configure Eclipse to pre-bundle App Engine classes into a single JAR for faster warm-up
I haven't done it that way as I'm not sure how to handle the passphrase authentication.
Take a look at Google Plugin for Eclipse.
It assembles all components required to build WAR files. Deployment must be done manually though.
I'm not sure if this is what you're talking about, but you can make a JAR out of an eclipse project by right-clicking the project, then choosing Export -> Java -> Jar file.
Make sure to choose the entry class/main class, or it won't work!

Categories

Resources