Export as runnable jar, but with editable property file? - java

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>

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.

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

putting a .java file into .war

My professor asks us to submit out projects in WAR files, and he says my project will get a 0 if the WAR doesn't include my .JAVA file.
I see it includes the .CLASS file but he said thats not enough, how can i get the .WAR file to pack with the .JAVA inside of it?
EDIT:
To compress the file i'm using the clean and build option in Netbeans. the WAR file appears in the build folder
If this is in Eclipse, you will include source under the export WAR option.
Update
In the war export panel, there should be an "Excluded file types" make sure to remove *.java
WAR file is just an archive, understandable with any ZIP archiver, like 7-zip. You can put anything you want into WAR.
Look for section "Including Source Files in the WAR File"
http://users.cis.fiu.edu/~downeyt/webdev/netbeans.html
quoted from the link:
From the properties screen, select Build -> Packaging. Edit the Exclude From WAR File box and remove */.java,.
What are you using to create the WAR file? If you use apache ant then add a copy task to your build.xml and then reference it in the default target e.g. with <target name="dist" depends="copy-source"/>
The task might look something like:
<target name="copy-source">
<echo>Copying source files...</echo>
<copy todir="${app.dir}/src">
<fileset dir="${javasrc.dir}">
<include name="**/*.java"/>
</fileset>
</copy>
</target>
ps. It is best to define the variables in an external build.properties file and never use hard-coded values in build.xml.
Also, have a look at http://ant.apache.org/manual/Tasks/war.html and http://ant.apache.org/manual/Tasks/copy.html
In Netbeans.Right click your project. Select Project Properties.It will open a Project properties window. Select Build->Packaging from the Categories Tab. In the window that appears on right hand side, there is a text field: Exclude from WAR file. In that text field default value is: /.java,/.form. In that text field remove the */.java value and click Ok.
Now clean and build project. Now you will find the war file exported with .java sources under dist directory of your project.

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