By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

Locating Jar Contents

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.