Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9090

Re: get path of a Jar file

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: get path of a Jar file
Date Sat, 22 Oct 2011 08:52:38 -0700 (PDT)
Organization http://groups.google.com
Lines 53
Message-ID <11617747.1232.1319298758994.JavaMail.geo-discussion-forums@yqjh13> (permalink)
References <j7u3th$9dr$1@online.de>
Reply-To comp.lang.java.programmer@googlegroups.com
NNTP-Posting-Host 72.14.225.65
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1319298859 14708 127.0.0.1 (22 Oct 2011 15:54:19 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sat, 22 Oct 2011 15:54:19 +0000 (UTC)
In-Reply-To <j7u3th$9dr$1@online.de>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=72.14.225.65; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9090

Show key headers only | View raw


Philipp Kraus wrote:
> I would like to extract a directory of a Jar file. I read the path of  
> my Jar file with:
> 
> String l_jarfile = 
> Class.forName("class_within_the_jar").getResource("").toString();
> l_jarfile        = l_jarfile.substring(9, 
> l_jarfile.lastIndexOf(".jar!")) + ".jar";
> 
> This does not work on MS Windows systems (unix and OSX work correct 
> with this code).
> If the Jar files is stored under a path like "C:\Users\myuser\Java 
> Files\myjar.jar" (with a space)
> the JarFile-Object can't locate the file, because the space within the 
> directory is changed to %20
> 
> I need a solution to detect the location of a Jar File in a class. How 
> can i [sic] Do this in a correct way?

Well, you could look for the "%20" I suppose, but your code looks messy and unnecessary.

Sometimes it pays to take a step back.  Just exactly why do you need the JAR file?  What does that give you that you cannot get any other way?

'Class.forName()' loads a class.  Every time you execute that line, you're attempting to load the class.  Why do you need to load the class again and again?  

Loading classes usually is only done when the class will be used in the program.  It only needs to be done once per class (per classloader).

Your variable names do not conform to the Java conventions for naming.  Don't use underscores in non-constant variables; use camel case ("camelCase") to distinguish word parts, with the first letter lower case for non-constant variables and for methods, upper case for types.  (Constant variables use ALL UPPER case with underscores to separate word parts instead of camel case.)

Your portability problem may be a consequence of your magic constants (9, ".jar!").  I don't know.  Are you sure they're the same for Windows?

Have you traced through log statements or a debugger what the various intermediate values are?  What are they?

But the real question is what do you want from this information?  I am not able to come up with a scenario where knowing the JAR path helps a program, not to retrieve resources or to load classes.  However, you seem to want it, so since the JAR is in your classpath, why don't you just look for matching substrings against the JAR names in your classpath?

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

get path of a Jar file Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-10-22 11:54 +0200
  Re: get path of a Jar file Lew <lewbloch@gmail.com> - 2011-10-22 08:52 -0700
  Re: get path of a Jar file Martin Gregorie <martin@address-in-sig.invalid> - 2011-10-22 21:19 +0000
  Re: get path of a Jar file Steven Simpson <ss@domain.invalid> - 2011-10-24 21:07 +0100
    Re: get path of a Jar file Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-10-30 18:49 +0100

csiph-web