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


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

Re: get path of a Jar file

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail
From Steven Simpson <ss@domain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: get path of a Jar file
Date Mon, 24 Oct 2011 21:07:18 +0100
Organization Aioe.org NNTP Server
Lines 40
Message-ID <m4tgn8-773.ln1@news.simpsonst.f2s.com> (permalink)
References <j7u3th$9dr$1@online.de>
NNTP-Posting-Host 3JnaVNumoybW/sOwRjlcjw.user.speranza.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Lightning/1.0b2 Thunderbird/3.1.15
X-Notice Filtered by postfilter v. 0.8.2
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9151

Show key headers only | View raw


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

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