473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6283
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan Gennick | last post by:
Alex Martelli, Anna Ravenscroft, and I are trying to locate the following people, whose Actionstate Python Cookbook recipes we are wishing to use in the second, printed edition of the Python...
0
by: Anne | last post by:
hie there, i have a couple of questions that i need help on. the first is regarding the closing of a browser.i have an exit button which on click i would like it to totally close the browser, and...
0
by: Johann Blake | last post by:
I'm having trouble grasping how ASP.NET correctly locates resources. There is plenty of documentation on this subject but some things are not clear at all. In my ASP.NET application, I have...
1
by: Radith | last post by:
TO ALL Remember in VB 6.0 we could do slight location adjustments using our arrow keys in the keyboard. How come you can’t do that in VB.NET; do you seriously have to specify pixel values in the...
1
by: Shiraz | last post by:
Hi It seems like none of the old posts get any follow up after a few messages, whether or not something constructive comes out of them, so I'm left with no choice but to repost an earlier...
0
by: kishore.sainath | last post by:
Hi All, The PHP 5.1.1 documentation says that NIS yp_ functions have been moved to the PECL repository and not bundled with PHP 5.1.1. However, I am unable to find it on the PECL site. Any...
5
by: BBands | last post by:
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in "beatlemania". The application is to look though a long...
2
by: Patrick Bates | last post by:
A year or two ago I saw an example on Microsoft's web site of an ASP.Net data bound control which worked something like this: Define the control, specify the number of columns you needed a table...
1
by: khandelwalk1 | last post by:
here is a java code, wich is supposed to read a file name from user n read its contents and display it, bt its nt working..ne1 dere cud help me...jus copy n paste dis code in a notepad file wid name...
4
by: Ty | last post by:
Hi all Short version of my problem: i have a Datagrid (Flexgrid from ComponentOne) with a Datatable as source. I need to search a row in the datatable, using a primary key column in the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.