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


Groups > comp.lang.java.programmer > #9087 > unrolled thread

get path of a Jar file

Started byPhilipp Kraus <philipp.kraus@flashpixx.de>
First post2011-10-22 11:54 +0200
Last post2011-10-30 18:49 +0100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#9087 — get path of a Jar file

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-10-22 11:54 +0200
Subjectget path of a Jar file
Message-ID<j7u3th$9dr$1@online.de>
Hello,

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 Do this in a correct way?

Thx

Phil

[toc] | [next] | [standalone]


#9090

FromLew <lewbloch@gmail.com>
Date2011-10-22 08:52 -0700
Message-ID<11617747.1232.1319298758994.JavaMail.geo-discussion-forums@yqjh13>
In reply to#9087
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

[toc] | [prev] | [next] | [standalone]


#9095

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2011-10-22 21:19 +0000
Message-ID<j7vc04$vse$1@localhost.localdomain>
In reply to#9087
On Sat, 22 Oct 2011 11:54:57 +0200, Philipp Kraus wrote:

One obvious thing to do is to get rid of the hex encoding for space 
before starting to work on the string:

   String jarfile =
      Class.forName("class_within_the_jar").getResource("").toString();

   jarfile = jarfile.replace("%20", " ");

But using a magic constant like 9 is asking for trouble 

>  jarfile = jarfile.substring(9, jarfile.lastIndexOf(".jar!")) + ".jar";

would be better written as:

   static final int START_POINT = 2  /* number of names to skip */

   String[] pathPieces = jarfile.split(File.separator);
   StringBuilder sb = new StringBuilder();
   int last = jarfile.length() - 1;
   for (int i = START_POINT; i < last; i++)
	sb.append(pathPieces[i] + File.separator);

   sb.append(pathPieces[last]);
   String relativePath = sb.toString();

which may be longer, but is a lot readable. It may even be portable 
between Windows and *NIX systems provided you want a relative path that, 
on a *NIX, corresponds to 

	~/Java\ Files\myjar.jar

but to 

	myuser\Java Files\myjar.jar
	
on Windows. Note that my solution uses nothing but String methods. If you 
haven't yet installed the SDK documentation where its readily available, 
I suggest you do so and get familiar with using it.
 
> I need a solution to detect the location of a Jar File in a class.
>
..but why do you want a relative path name rather than an absolute one? 
Both *NIX and Windows systems are happy with absolute names.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#9151

FromSteven Simpson <ss@domain.invalid>
Date2011-10-24 21:07 +0100
Message-ID<m4tgn8-773.ln1@news.simpsonst.f2s.com>
In reply to#9087
On 22/10/11 10:54, Philipp Kraus wrote:
> 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

Leaving aside the good questions of why, and whether these magic 
constants are a good thing, I will address the decoding of % sequences: 
Create a java.net.URI from the string, then create a java.io.File from 
the URI.

import java.net.*;
import java.io.*;

public class GetPath {
     public static void main(String[] args) throws Exception {
         Class<?>  clazz = Class.forName(args[0]);
         URL clazzRes = clazz.getResource("");
         String loc = clazzRes.toString();
         System.out.println("Resource: " + loc);
         URI stripped =
             URI.create(loc.substring(4, loc.lastIndexOf(".jar!") + 4));
         System.out.println("Stripped: " + stripped);
         File result = new File(stripped);
         System.out.println("Result: " + result);
     }
}

Lots of result checking omitted.

-- 
ss at comp dot lancs dot ac dot uk

[toc] | [prev] | [next] | [standalone]


#9317

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-10-30 18:49 +0100
Message-ID<j8k2o4$d5e$1@online.de>
In reply to#9151
On 2011-10-24 22:07:18 +0200, Steven Simpson said:

> On 22/10/11 10:54, Philipp Kraus wrote:
>> 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
> 
> Leaving aside the good questions of why, and whether these magic 
> constants are a good thing, I will address the decoding of % sequences: 
> Create a java.net.URI from the string, then create a java.io.File from 
> the URI.
> 
> import java.net.*;
> import java.io.*;
> 
> public class GetPath {
>      public static void main(String[] args) throws Exception {
>          Class<?>  clazz = Class.forName(args[0]);
>          URL clazzRes = clazz.getResource("");
>          String loc = clazzRes.toString();
>          System.out.println("Resource: " + loc);
>          URI stripped =
>              URI.create(loc.substring(4, loc.lastIndexOf(".jar!") + 4));
>          System.out.println("Stripped: " + stripped);
>          File result = new File(stripped);
>          System.out.println("Result: " + result);
>      }
> }
> 
> Lots of result checking omitted.

Thanks, the URI option is imho the best solution

Phil

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web