Adding the current directory to a JAR file's classpath - java

My code is being deployed as a JAR file. The JAR contains a directory lib which contains a number of third-party JARs which my code requires. I've added these to the classpath, using Ant, via the MANIFEST.MF.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 20.5-b03 (Sun Microsystems Inc.)
Main-Class: com.package.Class
Class-Path: ../lib/c3p0-0.9.1.2.jar ../lib/dbunit-2.4.8.jar ../lib/gua
va-10.0.1.jar ../lib/hsqldb.jar ../lib/javax.jms.jar ../lib/log4j-1.2
.16.jar ../lib/mockito-all-1.9.0.jar ../lib/ojdbc14.jar ../lib/slf4j-
api-1.6.4.jar ../lib/slf4j-log4j12-1.6.4.jar ../lib/xmlunit-1.3.jar
There is also a queries.properties file which is in the root of the JAR.
There are two further properties files which are required. I would like these to reside in the same directory as the JAR file and for the code to be able to locate them. I believe for the code to be able to locate these properties files, they need to be in the classpath. I therefore need to add the JAR file's directory to the classpath.
Firstly, is this the correct approach of should I use an alternative means of locating the properties files?
If this is correct, how do I use Ant to add the JAR's current directory to the classpath in MANIFEST.MF? I added the JARs in the lib directory to the classpath using the manifestclasspath Ant task.

Have you tried adding the . as a reference to the current directory?
<jar jarfile="dummy.jar">
<manifest>
<attribute name="Main-Class" value="com.package.Class"/>
<attribute name="Class-Path" value=". ${jar.class.path}"/>
</manifest>
</jar>
Please also see Executable jar won't find the properties files

Related

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>

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

How to build an executable jar file with Ant

I'm developing Swing based application in Java I want a executable JAR file for this project.
All external library files used in the project should be packaged in the JAR file.
How can I build a runnable JAR file using ANT?
but it needs all external library
files used in the project should be
along with the jar.
Of course, but the external JARS should not be bundled in with the executable JAR.
You need to do three things:
Create a manifest for your executable JAR that specifies the Main-Class.
Add the CLASSPATH to you manifest that spells out the location of each and every dependent JAR relative to the executable JAR.
Create a ZIP file that contains the executable JAR and all the dependent JARs, with paths relative to the executable JAR as specified in your manifest.
You give clients the ZIP. They unpack it and execute your executable JAR.
You need to include the manifest task in your jar task. The manifest references the main class to be executed by default when you start your jar.
it could look like this :
<jar...
<manifest file="MANIFEST.MF">
<attribute name="Main-Class" value="com.example.YourMainClass"/>
</manifest>
</jar>

Create JAR file without including external dependencies

Is it possible to create a JAR file that requires external dependencies without including those dependencies in the JAR file?
My google-fu has failed to give me an answer; everything that I have found shows how to include them in the JAR file, but not what to put in the manifest file to say "I haven't got them, look in the user's classpath". I would assume that the dependencies are properly installed and configured on the user's classpath.
In my case, my dependencies are Apache Commons CLI and Math.
Edit:
Inside my JAR file, I have Main.class.
My manifest file looks like:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: Main
My CLASSPATH looks like
.;C:\Program Files\Java\jre1.6.0_06\lib\ext\QTJava.zip;C:\java_lib\commons-cli-1.2.jar;C:\java_lib\commons-math-2.0\commons-math-2.0.jar
If I include the dependencies in the JAR in /lib and add the line Class-Path: lib/commons-math-2.0.jar lib/commons-cli-1.2.jar to the manifest, then it does work.
I've tried adding Class-Path: commons-math-2.0.jar commons-cli-1.2.jar to the manifest without including the files in the JAR just to see if that would work, but it didn't.
Use the Class-Path entry in the META-INF/MANIFEST.MF to tell where to look for dependencies relatively to your JAR. For example:
Class-Path: servlet.jar ../foo/bar.jar acme/beans.jar
It certainly is possible. One way to think about it is that every time you create a jar, you are depending on the classes in the jre, and it is not necessary to include them in your jar. The jre will automatically look for them in the classpath. If they are not found you will see a NoClassDefFoundError.

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