470,849 Members | 1,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Locating Jar Contents

Dear news group friends,

I am new to Java, only been working with it for a month or so. Here is
my problem:

I need to package my tree of classes into a Jar file. One of those
classes must be able to determine at run time what Jar file it came
from, and more importantly where that Jar file is located at. This is
because it must pass the Jar file path and file name to another class
that searches the Jar file for a data file located in the root folder
in the Jar file.

Does anyone know an easy way to have a class find the location of the
Jar file it came from? Any help would be wonderful.

Greg Ofiesh
Jul 17 '05 #1
3 6168
Greg Ofiesh wrote:
Dear news group friends,

I am new to Java, only been working with it for a month or so. Here is
my problem:

I need to package my tree of classes into a Jar file. One of those
classes must be able to determine at run time what Jar file it came
from, and more importantly where that Jar file is located at. This is
because it must pass the Jar file path and file name to another class
that searches the Jar file for a data file located in the root folder
in the Jar file.

Does anyone know an easy way to have a class find the location of the
Jar file it came from? Any help would be wonderful.

Greg Ofiesh


Greg,

Assuming you are using the default class loader and not anything funky
(a J2EE environment would probably qualify as "funky"), you could use
the Class.getResource() method to get the URL for the class file:

// Start ResourceExample.java
public class ResourceExample
{
public static void main(String args[])
{
System.out.println(ResourceExample.class.getResour ce(
"ResourceExample.class"));
}
}
// End ResourceExample.java
# Start example output
[tmp]$ java -cp . ResourceExample
file:/tmp/ResourceExample.class
[tmp]$ jar cvfm re.jar re.mf ResourceExample.class
added manifest
adding: ResourceExample.class(in = 937) (out= 533)(deflated 43%)
[tmp]$ java -jar re.jar
jar:file:/tmp/re.jar!/ResourceExample.class
# End example output
Of course, after rereading your original post, you may be able to do
this by just using ClassLoader.getResource() directly in your second
class. For example, if you needed "xyz.txt" from the jar file you could do

URL res = this.getClass().getClassLoader().getResource("/xyz.txt");

The only potential issue is if you have multiple "xyz.txt" files in the
CLASSPATH.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2
gr********@yahoo.com (Greg Ofiesh) wrote in
news:d9**************************@posting.google.c om:
Dear news group friends,

I am new to Java, only been working with it for a month or so. Here is
my problem:

I need to package my tree of classes into a Jar file. One of those
classes must be able to determine at run time what Jar file it came
from, and more importantly where that Jar file is located at. This is
because it must pass the Jar file path and file name to another class
that searches the Jar file for a data file located in the root folder
in the Jar file.

Does anyone know an easy way to have a class find the location of the
Jar file it came from? Any help would be wonderful.

Greg Ofiesh


Hi Greg,

Searching for a solution for a different problem I saw this (yet
unaswered?) question.

You might try to get the URL to your class from the ClassLoader. From
this URL you might extract the path to that JAR file. Like this:

import java.net.*;

URL url = ClassLoader.getSystemResource("com/mycompany/MyClass.class");
// note that you must type slashes and not dots
System.out.println("path to my class: " + url.toString());

As you hopefully will see, there should be the path you are looking for.

(I did not test this as I wrote it down, but I did a similar thing with
some other file and that worked)

Piet
Jul 17 '05 #3
gr********@yahoo.com (Greg Ofiesh) wrote in
news:d9**************************@posting.google.c om:
Dear news group friends,

I am new to Java, only been working with it for a month or so. Here is
my problem:

I need to package my tree of classes into a Jar file. One of those
classes must be able to determine at run time what Jar file it came
from, and more importantly where that Jar file is located at. This is
because it must pass the Jar file path and file name to another class
that searches the Jar file for a data file located in the root folder
in the Jar file.

Does anyone know an easy way to have a class find the location of the
Jar file it came from? Any help would be wonderful.

Greg Ofiesh


Hi Greg,

Searching for a solution for a different problem I saw this (yet
unaswered?) question.

You might try to get the URL to your class from the ClassLoader. From
this URL you might extract the path to that JAR file. Like this:

import java.net.*;

URL url = ClassLoader.getSystemResource("com/mycompany/MyClass.class");
// note that you must type slashes and not dots
System.out.println("path to my class: " + url.toString());

As you hopefully will see, there should be the path you are looking for.

(I did not test this as I wrote it down, but I did a similar thing with
some other file and that worked)

Piet
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jonathan Gennick | last post: by
reply views Thread by Anne | last post: by
1 post views Thread by Radith | last post: by
1 post views Thread by Shiraz | last post: by
reply views Thread by kishore.sainath | last post: by
5 posts views Thread by BBands | last post: by
2 posts views Thread by Patrick Bates | last post: by
1 post views Thread by khandelwalk1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.