Runnable JAR file export - java

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

Related

how to combine jar files from dist folder and jar files in lib within dist folder to make a single jar file using build.xml

How does one combine jar files from dist folder and jar files in lib within dist folder to make a single jar file using build.xml. Currently I use the following code to make single jar, but it will show the message "A java exception has occurred".
.
<?xml version="1.0" encoding="UTF-8"?>
<project name="CIH_Billing" default="jar">
<target name="jar">
<zip destfile="output.jar">
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<zipgroupfileset dir="dist" includes="*.jar"/>
</zip>
</target>
</project>
You don't. Valid JAR files cannot contain other JAR files. If you really, really want to do this there are ways, but you need to think long and hard about the reasons for justifying this. Using something like Jar Jar Links for instance, while raising other issues does do what you want. Alternatively, you can extract all the jars and combine them manually, which also has it's drawbacks.
However as a general rule of thumb java applications are distributed as a bundle and launched with a small batch script (which sets the classpath and launches the main method). If you don't want to do it this way, but want a standalone jar, there is always Jar Jar Links, if you want a stand alone Windows binary. you can always use something like Excelsior JET or Avian.
Using either with Ant though was somewhat complicated last time I checked.

Appending attributes to manifest.mf of a JAR

My application needs me to add some atributes to manifest.mf of different jars without changing the previous version of manifest.mf. In short i want to append some attributes to manifest.mf of JAR. I am using ANT to build my application. In JAVA i can do this while creating the jar with command
jar cfm jar-file manifest-addition input-file(s) given here But i want to do this with an ANT Task.
Is there any ANT task i can use for this? if not then the only solution left with me is
Unzip the jar
Update the MANIFEST.MF
JAR up the content again
This seems to be a lengthy process.
Please suggest.
You could use the <exec> task to run that command line through Ant.
i know you want to add the content of additional files, but maybe you
only need to add simple attributes, this would help
<jar destfile="${web.home}/signapplet.jar"
basedir="${build.home}/applet/signer/classes">
<manifest>
<attribute name="Permissions" value="all-permissions"/>
</manifest>
</jar>

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>

create jar for webapp

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.

How do I attach properties files to a jar?

I have a project that uses the serial port, and it requires two files to run, the win32.dll file (which is in the java runtime environment bin folder) and the javax.comm.properties file (which is in the java runtime environment lib folder). When I run the project from eclipse it works, but when I try to build a jar file for distribution, it won't work. I suspect this is because the dll and properties files aren't included in the jar. How do I specify that they need to be there?
You generally don't put dll and properties files inside the jar. Properties files as well other jar files need to be added to the classpath. The jar file has a manifest file that defines the classpath used. You can't edit this with eclipse. You need to define an ant build.xml file and do something like this:
<jar jarfile="${dist}/MyJar.jar" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="MyClass"/>
<attribute name="Class-Path" value="."/>
</manifest>
</jar>
Then put the properties file in the same folder as the jar. You can run the ant target by right clicking the build.xml and selecting the "Run as Ant target".
If I remember correctly, placing the dll file in the bin directory of the jre will work.
I think javax.comm.properties just need to be on your classpath. You may can add it to the top level of a jar you delivery.
InputStream is = MainClass.class.getResourceAsStream("javax.comm.properties");
if (is == null) {properties missing....}
I think win32.dll just need to be on the %PATH%(windows) or $LD_LIBRARY_PATH(unix)......
A jar file is just a normal zip file. If you want to add files to it, just use a tool such as winzip.
With Ant, you can pack everything in your Jar you want to. So let Ant create your Jar, not Eclipse :)

Categories

Resources