Get directory path when running java jar [duplicate] - java

I want to read an XML file that is located inside one of the jars included in my class path. How can I read any file which is included in the jar?

If you want to read that file from inside your application use:
InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");
The path starts with "/", but that is not the path in your file-system, but in your classpath. So if your file is at the classpath "org.xml" and is called myxml.xml your path looks like "/org/xml/myxml.xml".
The InputStream reads the content of your file. You can wrap it into an Reader, if you want.

Ah, this is one of my favorite subjects. There are essentially two ways you can load a resource through the classpath:
Class.getResourceAsStream(resource)
and
ClassLoader.getResourceAsStream(resource)
(there are other ways which involve getting a URL for the resource in a similar fashion, then opening a connection to it, but these are the two direct ways).
The first method actually delegates to the second, after mangling the resource name. There are essentially two kinds of resource names: absolute (e.g. "/path/to/resource/resource") and relative (e.g. "resource"). Absolute paths start with "/".
Here's an example which should illustrate. Consider a class com.example.A. Consider two resources, one located at /com/example/nested, the other at /top, in the classpath. The following program shows nine possible ways to access the two resources:
package com.example;
public class A {
public static void main(String args[]) {
// Class.getResourceAsStream
Object resource = A.class.getResourceAsStream("nested");
System.out.println("1: A.class nested=" + resource);
resource = A.class.getResourceAsStream("/com/example/nested");
System.out.println("2: A.class /com/example/nested=" + resource);
resource = A.class.getResourceAsStream("top");
System.out.println("3: A.class top=" + resource);
resource = A.class.getResourceAsStream("/top");
System.out.println("4: A.class /top=" + resource);
// ClassLoader.getResourceAsStream
ClassLoader cl = A.class.getClassLoader();
resource = cl.getResourceAsStream("nested");
System.out.println("5: cl nested=" + resource);
resource = cl.getResourceAsStream("/com/example/nested");
System.out.println("6: cl /com/example/nested=" + resource);
resource = cl.getResourceAsStream("com/example/nested");
System.out.println("7: cl com/example/nested=" + resource);
resource = cl.getResourceAsStream("top");
System.out.println("8: cl top=" + resource);
resource = cl.getResourceAsStream("/top");
System.out.println("9: cl /top=" + resource);
}
}
The output from the program is:
1: A.class nested=java.io.BufferedInputStream#19821f
2: A.class /com/example/nested=java.io.BufferedInputStream#addbf1
3: A.class top=null
4: A.class /top=java.io.BufferedInputStream#42e816
5: cl nested=null
6: cl /com/example/nested=null
7: cl com/example/nested=java.io.BufferedInputStream#9304b1
8: cl top=java.io.BufferedInputStream#190d11
9: cl /top=null
Mostly things do what you'd expect. Case-3 fails because class relative resolving is with respect to the Class, so "top" means "/com/example/top", but "/top" means what it says.
Case-5 fails because classloader relative resolving is with respect to the classloader. But, unexpectedly Case-6 also fails: one might expect "/com/example/nested" to resolve properly. To access a nested resource through the classloader you need to use Case-7, i.e. the nested path is relative to the root of the classloader. Likewise Case-9 fails, but Case-8 passes.
Remember: for java.lang.Class, getResourceAsStream() does delegate to the classloader:
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResourceAsStream(name);
}
return cl.getResourceAsStream(name);
}
so it is the behavior of resolveName() that is important.
Finally, since it is the behavior of the classloader that loaded the class that essentially controls getResourceAsStream(), and the classloader is often a custom loader, then the resource-loading rules may be even more complex. e.g. for Web-Applications, load from WEB-INF/classes or WEB-INF/lib in the context of the web application, but not from other web-applications which are isolated. Also, well-behaved classloaders delegate to parents, so that duplicateed resources in the classpath may not be accessible using this mechanism.

Check first your class loader.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = Class.class.getClassLoader();
}
classLoader.getResourceAsStream("xmlFileNameInJarFile.xml");
// xml file location at xxx.jar
// + folder
// + folder
// xmlFileNameInJarFile.xml

A JAR is basically a ZIP file so treat it as such. Below contains an example on how to extract one file from a WAR file (also treat it as a ZIP file) and outputs the string contents. For binary you'll need to modify the extraction process, but there are plenty of examples out there for that.
public static void main(String args[]) {
String relativeFilePath = "style/someCSSFile.css";
String zipFilePath = "/someDirectory/someWarFile.war";
String contents = readZipFile(zipFilePath,relativeFilePath);
System.out.println(contents);
}
public static String readZipFile(String zipFilePath, String relativeFilePath) {
try {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement();
// if the entry is not directory and matches relative file then extract it
if (!entry.isDirectory() && entry.getName().equals(relativeFilePath)) {
BufferedInputStream bis = new BufferedInputStream(
zipFile.getInputStream(entry));
// Read the file
// With Apache Commons I/O
String fileContentsStr = IOUtils.toString(bis, "UTF-8");
// With Guava
//String fileContentsStr = new String(ByteStreams.toByteArray(bis),Charsets.UTF_8);
// close the input stream.
bis.close();
return fileContentsStr;
} else {
continue;
}
}
} catch (IOException e) {
logger.error("IOError :" + e);
e.printStackTrace();
}
return null;
}
In this example I'm using Apache Commons I/O and if you are using Maven here is the dependency:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

Just for completeness, there has recently been a question on the Jython mailinglist where one of the answers referred to this thread.
The question was how to call a Python script that is contained in a .jar file from within Jython, the suggested answer is as follows (with "InputStream" as explained in one of the answers above:
PythonInterpreter.execfile(InputStream)

This also works on spring
ClassPathResource resource = new ClassPathResource("/file.txt", MainApplication.class); //resources folder
InputStream inputStream = resource.getInputStream();
File file = new File("file.txt");
FileUtils.copyInputStreamToFile(inputStream, file);

Related

ImageIO.read can't find Files that are moved to a subfolder [duplicate]

I want to read an XML file that is located inside one of the jars included in my class path. How can I read any file which is included in the jar?
If you want to read that file from inside your application use:
InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");
The path starts with "/", but that is not the path in your file-system, but in your classpath. So if your file is at the classpath "org.xml" and is called myxml.xml your path looks like "/org/xml/myxml.xml".
The InputStream reads the content of your file. You can wrap it into an Reader, if you want.
Ah, this is one of my favorite subjects. There are essentially two ways you can load a resource through the classpath:
Class.getResourceAsStream(resource)
and
ClassLoader.getResourceAsStream(resource)
(there are other ways which involve getting a URL for the resource in a similar fashion, then opening a connection to it, but these are the two direct ways).
The first method actually delegates to the second, after mangling the resource name. There are essentially two kinds of resource names: absolute (e.g. "/path/to/resource/resource") and relative (e.g. "resource"). Absolute paths start with "/".
Here's an example which should illustrate. Consider a class com.example.A. Consider two resources, one located at /com/example/nested, the other at /top, in the classpath. The following program shows nine possible ways to access the two resources:
package com.example;
public class A {
public static void main(String args[]) {
// Class.getResourceAsStream
Object resource = A.class.getResourceAsStream("nested");
System.out.println("1: A.class nested=" + resource);
resource = A.class.getResourceAsStream("/com/example/nested");
System.out.println("2: A.class /com/example/nested=" + resource);
resource = A.class.getResourceAsStream("top");
System.out.println("3: A.class top=" + resource);
resource = A.class.getResourceAsStream("/top");
System.out.println("4: A.class /top=" + resource);
// ClassLoader.getResourceAsStream
ClassLoader cl = A.class.getClassLoader();
resource = cl.getResourceAsStream("nested");
System.out.println("5: cl nested=" + resource);
resource = cl.getResourceAsStream("/com/example/nested");
System.out.println("6: cl /com/example/nested=" + resource);
resource = cl.getResourceAsStream("com/example/nested");
System.out.println("7: cl com/example/nested=" + resource);
resource = cl.getResourceAsStream("top");
System.out.println("8: cl top=" + resource);
resource = cl.getResourceAsStream("/top");
System.out.println("9: cl /top=" + resource);
}
}
The output from the program is:
1: A.class nested=java.io.BufferedInputStream#19821f
2: A.class /com/example/nested=java.io.BufferedInputStream#addbf1
3: A.class top=null
4: A.class /top=java.io.BufferedInputStream#42e816
5: cl nested=null
6: cl /com/example/nested=null
7: cl com/example/nested=java.io.BufferedInputStream#9304b1
8: cl top=java.io.BufferedInputStream#190d11
9: cl /top=null
Mostly things do what you'd expect. Case-3 fails because class relative resolving is with respect to the Class, so "top" means "/com/example/top", but "/top" means what it says.
Case-5 fails because classloader relative resolving is with respect to the classloader. But, unexpectedly Case-6 also fails: one might expect "/com/example/nested" to resolve properly. To access a nested resource through the classloader you need to use Case-7, i.e. the nested path is relative to the root of the classloader. Likewise Case-9 fails, but Case-8 passes.
Remember: for java.lang.Class, getResourceAsStream() does delegate to the classloader:
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResourceAsStream(name);
}
return cl.getResourceAsStream(name);
}
so it is the behavior of resolveName() that is important.
Finally, since it is the behavior of the classloader that loaded the class that essentially controls getResourceAsStream(), and the classloader is often a custom loader, then the resource-loading rules may be even more complex. e.g. for Web-Applications, load from WEB-INF/classes or WEB-INF/lib in the context of the web application, but not from other web-applications which are isolated. Also, well-behaved classloaders delegate to parents, so that duplicateed resources in the classpath may not be accessible using this mechanism.
Check first your class loader.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = Class.class.getClassLoader();
}
classLoader.getResourceAsStream("xmlFileNameInJarFile.xml");
// xml file location at xxx.jar
// + folder
// + folder
// xmlFileNameInJarFile.xml
A JAR is basically a ZIP file so treat it as such. Below contains an example on how to extract one file from a WAR file (also treat it as a ZIP file) and outputs the string contents. For binary you'll need to modify the extraction process, but there are plenty of examples out there for that.
public static void main(String args[]) {
String relativeFilePath = "style/someCSSFile.css";
String zipFilePath = "/someDirectory/someWarFile.war";
String contents = readZipFile(zipFilePath,relativeFilePath);
System.out.println(contents);
}
public static String readZipFile(String zipFilePath, String relativeFilePath) {
try {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement();
// if the entry is not directory and matches relative file then extract it
if (!entry.isDirectory() && entry.getName().equals(relativeFilePath)) {
BufferedInputStream bis = new BufferedInputStream(
zipFile.getInputStream(entry));
// Read the file
// With Apache Commons I/O
String fileContentsStr = IOUtils.toString(bis, "UTF-8");
// With Guava
//String fileContentsStr = new String(ByteStreams.toByteArray(bis),Charsets.UTF_8);
// close the input stream.
bis.close();
return fileContentsStr;
} else {
continue;
}
}
} catch (IOException e) {
logger.error("IOError :" + e);
e.printStackTrace();
}
return null;
}
In this example I'm using Apache Commons I/O and if you are using Maven here is the dependency:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Just for completeness, there has recently been a question on the Jython mailinglist where one of the answers referred to this thread.
The question was how to call a Python script that is contained in a .jar file from within Jython, the suggested answer is as follows (with "InputStream" as explained in one of the answers above:
PythonInterpreter.execfile(InputStream)
This also works on spring
ClassPathResource resource = new ClassPathResource("/file.txt", MainApplication.class); //resources folder
InputStream inputStream = resource.getInputStream();
File file = new File("file.txt");
FileUtils.copyInputStreamToFile(inputStream, file);

getResourceAsStream() - InputStream null when using relative path

I am using Maven and running a JUnit test on a static method that tries to read in a file using:
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
And then using new InputStreamReader(is) to use as a reader in another call. This works when filename is just a filename (e.g. file.csv) but when filename is a relative path (e.g. src/test/resources/file.csv), is ends up being null.
I need it to handle relative paths, because I'm running a test suite via JUnit that looks for resources with relative paths and these tests are coming from a JAR that I have no control over changing (i.e. I implemented a facade implementation class that the test suite uses to call its own tests with its own resources - they are out of my control).
Is there a way for this approach to work with relative paths, or some other way that I can find those resources on my classpath that the tests are looking for?
Running tests in maven, src/test/resources/ is (by default) "mapped" to the root of the class(loader)path, so in your case /file.csv is the correct absolute path of src/test/resources/file.csv.
To load "src/test/resources/file.csv" (Resource) successfully (which is comple nonsense), you should have this file (physically) available: src/test/resources/src/test/resources/file.csv, or respectively src/main/java(which would also be mapped to cp root) .../src/test/resources/file.csv
In your case is ends up being null because when a resource lookup is performed, the resources directory is entry point, so the complete path is expected to be test/resources/src/test/resources/file.csv or test/resources/src/test/resources/file.csv depending on how you run your tests.
The only reliable way you could achieve your goal is to take care about the correctness manually. Perhaps something like this:
static String getResourceFileName(String maybePath) {
Path path = Paths.get(maybePath);
if (path.getNameCount() > 0) {
return path.getFileName().toString();
} else {
throw new IllegalArgumentException("Can't derive filename given " + maybePath);
}
}
#Test
public void testSoq() throws Exception {
String fileName0 = getResourceFileName("file");
InputStream is0 = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName0);
String fileName1 = getResourceFileName("src/test/resources/file");
InputStream is1 = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName1);
String string0 = new BufferedReader(new InputStreamReader(is0)).readLine();
String string1 = new BufferedReader(new InputStreamReader(is1)).readLine();
assertEquals(string0, string1); // OK
}
Maybe you'll have to write it according to the specifics of your case (account for possible nested diretories etc.), but I really think that given your inputs may be of arbitrary nature, this won't get much simpler.
I have this on blog (in spanish)
http://lacuevadeyogui.blogspot.com/2015/04/diferencia-entre-uri-y-url.html
The problem in with the reference to file in src/test/resources
try this
URL url = getClass().getClassLoader().getResource("file.csv");
File f;
try {
f = new File(url.toURI());
} catch (URISyntaxException e) {
f = new File(url.getPath());
}
At the end, convert file to inputStream.

Java read file within static method, using ClassLoader gives FileNotFoundException

I want to read a file in my java class. My question is similar to this one, but there are two differences. first, I use a different project layout:
/src/com/company/project
/resources
In the resources folder I have a file called "test.txt":
/resources/test.txt
In the project folder I have a class test.java
/src/com/company/project/test.java
I want mu java class to be able to read the contents of test.txt in a STATIC METHOD. I've tried the following:
private static String parseFile()
{
try
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String fileURL = classLoader.getResource("test.txt").getFile();
File file = new File(fileURL);
...
}
}
and the following paths:
File file1 = new File("test.txt");
File file2 = new File("/test.txt");
File file3 = new File("/resources/test.txt");
But they all throw a FileNotFoundException when I want to read the file. How can I correctly declare the path to my file in the snippet above with respect to my project setup and the fact that the method needs to be static?
You should use the class loader of the class which is in the same JAR as the resource instead of the TCCL. And then you need to specify the name of the resource with a full path. And it is typically not good to access those as files. Just open it directly for read (or copy it to a temp file if you need to):
InputStream is =
Project.class.getClassLoader().getResourceAsStream("/resource/test.txt");
BTW: if you simply want to open a file, you need to use a relative file name. This is searched relative to the start dir, which is normally the project main dir (in eclipse):
File resource = new File("resource/test.txt");
(but this wont work if you package it up as a JAR).
After endless trials, I gave up on ClassLoader and getResource methods of any kind.
Absolutely nothing worked, especially if the opening attempt was made from another project. I always ended up getting the bin folder instead of the src folder.
So I devised the following work around:
public class IOAccessory {
public static String getProjectDir() {
try {
Class<?> callingClass = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName());
URL url = callingClass.getProtectionDomain().getCodeSource().getLocation();
URI parentDir = url.toURI().resolve("..");
return parentDir.getPath();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return "";
}
}
The getProjectDir method returns the physical path of the project from which it was called, e.g. C:/workspace/MyProject/.
After that, all you need to do is concatenate the relative path in MyProject of your resource file to open the stream:
public void openResource() throws IOException {
InputStream stream = null;
String projectDir = IOAccessory.getProjectDir();
String filePath = "resources/test.txt";
try {
stream = new FileInputStream(projectDir + filePath);
open(stream);
} catch(Exception e) {
e.printStackTrace();
} finally {
if (stream != null)
stream.close();
}
}
This technique works whether the openResource method is static or non-static, and whether it is called from within the project or from another project on the build path.
It really depends on how your IDE generates output from your project. Typically, classloaders load resources relative to the invoking classes, but if treated right, 'resources' will just end up in the 'root' of your output folder hierarchy, and you can access them accordingly.
For example, if I recreate your code in IntelliJ IDEA, in a class called com/acme/TestClass.class, the following output structure is generated within the IDE when building. This assumes I have "test.txt" sitting in a folder I called "resources", and that folder is specified as being a "resources root":
/com
/acme
TestClass.class
test.txt
The text file ends up in the output folder's root, so accessing it is simple. The following code works for me when I attempt to load the file in a static method within TestClass:
ClassLoader cl = TestClass.class.getClassLoader();
InputStream is = cl.getResourceAsStream("test.txt");
The only thing not covered in the other answers is that your URL conversion to file might not work correctly. If the directories above your project contain a characters that must be decoded then your call to 'getResource("test.txt").getFile()' is not giving you a valid java.io.File path.
I load shader for openGL ES from static function.
Remember you must use lower case for your file and directory name, or else the operation will be failed
public class MyGLRenderer implements GLSurfaceView.Renderer {
...
public static int loadShader() {
// Read file as input stream
InputStream inputStream = MyGLRenderer.class.getResourceAsStream("/res/raw/vertex_shader.txt");
// Convert input stream to string
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String shaderCode = s.hasNext() ? s.next() : "";
}
...
}
Another method to convert input stream to string.
byte[] bytes;
String shaderCode = "";
try {
bytes = new byte[inputStream.available()];
inputStream.read(bytes);
shaderCode = new String(bytes);
}
catch (IOException e) {
e.printStackTrace();
}

Where to put a file to read from a class under a package in java?

I have a properties file contains the file name only say file=fileName.dat. I've put the properties file under the class path and could read the file name(file.dat) properly from it in the mainClass. After reading the file name I passed the file name(just name not the path) to another class under a package say pack.myClass to read that file. But the problem is pack.myClass could not get the file path properly. I've put the file fileName.dat both inside and outside the packagepack but couldn't make it work.
Can anybody suggest me that where to put the file fileName.dat so I can read it properly and the whole application would be portable too.
Thanks!
The code I'm using to read the config file and getting the file name:
Properties prop = new Properties();
InputStream in = mainClass.class.getResourceAsStream("config.properties");
prop.load(in);
in.close();
myClass mc = new myClass();
mc.readTheFile(prop.getProperty("file"));
/*until this code is working good*/
Then in myClass which is under package named pack I am doing:
public void readTheFile(String filename) throws IOException {
FileReader fileReader = new FileReader(filename); /*this couldn't get the file whether i'm putting the file inside or outside the package folder */
/*after reading the file I've to do the BufferReader for further operation*/
BufferedReader bufferedReader = new BufferedReader(fileReader);
I assume that you are trying to read properties file using getResource method of class. If you put properties file on root of the classpath you should prefix file name with '/' to indicate root of classpath, for example getResource("/file.dat"). If properties file is under the same folder with the class you on which you invoke getResource method, than you should not use '/' prefix.
When you use a relative file name such as fileName.dat, you're asking for a file with this name in the current directory. The current directory has nothing to do with packages. It's the directory from which the JVM is started.
So if you're in the directory c:\foo\bar when you launch your application (using java -cp ... pack.MyClass), it will look for the file c:\foo\bar\fileName.dat.
Try..
myClass mc = new myClass();
InputStream in = mc.getClass().getResourceAsStream("/pack/config.properties");
..or simply
InputStream in = mc.getClass().getResourceAsStream("config.properties");
..for the last line if the main is in myClass The class loader available in the main() will often be the bootstrap class-loader, as opposed to the class-loader intended for application resources.
Class.getResource will look in your package directory for a file of the specified name.
JavaDocs here
Or getResourceAsStream is sometimes more convenient as you probably want to read the contents of the resource.
Most of the time it would be best to look for the "fileName.dat" somewhere in the "user.home" folder, which is a system property. First create a File path from the "user.home" and then try to find the file there. This is a bit of a guess as you don't provide the exact user of the application, but this would be the most common place.
You are currently reading from the current folder which is determined by
String currentDir = new File(".").getAbsolutePath();
or
System.getProperty("user.dir")
To read a file, even from within a jar archive:
readTheFile(String package, String filename) throws MalformedURLException, IOException
{
String filepath = package+"/"+filename;
// like "pack/fileName.dat" or "fileName.dat"
String s = (new SourceBase()).getSourceBase() + filepath;
URL url = new URL(s);
InputStream ins = url.openStream();
BufferedReader rdr = new BufferedReader(new InputStreamReader(ins, "utf8"));
do {
s = rdr.readLine();
if(s!= null) System.out.println(s);
}
while(s!=null);
rdr.close();
}
with
class SourceBase
{
public String getSourceBase()
{
String cn = this.getClass().getName().replace('.', '/') + ".class";
// like "packagex/SourceBase.class"
String s = this.getClass().getResource('/' + cn).toExternalForm();
// like "file:/javadir/Projects/projectX/build/classes/packagex/SourceBase.class"
// or "jar:file:/opt/java/PROJECTS/testProject/dist/
// testProject.jar!/px/SourceBase.class"
return s.substring(0, s.lastIndexOf(cn));
// like "file:/javadir/Projects/projectX/build/classes/"
// or "jar:file:/opt/java/PROJECTS/testProject/dist/testProject.jar!/"
}
}

Location of javaagent jar in bootclasspath

I have a javaagent jar that I put on the bootclasspath using
Boot-Class-Path: myagent.jar
inside the MANIFEST.MF file.
I need to find out the directory on the filesystem in which the jar is located.
However the method described for this here doesnt seem to work for me:
new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().gĀ­etPath());
In this case the ProtectionDomain.getCodeSource() returns null. I guess this is happening because the jar has been put on the boot classpath. Because of this I also cannot do MyClass.getClassLoader() to get the resource location.
I am using Java 6.
Can anyone tell how to get the location of the jar?
You can use the System class loader to find classes on the boot class path. For example, this
System.out.println(
ClassLoader.getSystemClassLoader().getResource("java/lang/String.class")
);
Will print out something like,
jar:file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/lib/rt.jar!/java/lang/String.class
To find the location of MyClass.class on disk, do
String urlString = ClassLoader
.getSystemClassLoader()
.getResource("com/my/package/MyClass.class")
.toString();
urlString = urlString.substring(urlString.indexOf("file:"), urlString.indexOf('!'));
URL url = new URL(urlString);
File file = new File(url.toURI());
System.out.println(file);
System.out.println(file.exists());
Update 2020-06-29 by kriegaex: Rather than writing a new answer to this old question, I want to enhance or update this very good answer, also taking account paths with spaces and Java 9+ modules.
Here is a method returning the class file path as a File instance. If a class file is part of a JAR or Java runtime module (URL starts with protocol jrt:), it just returns the paths to the JAR or to the JMOD file, which is something you might not want, but it is just a showcase you can edit to your heart's content. Of course this is still hacky, e.g. not considering classes loaded from a web URL instead of from file, but you get the idea.
public static File getFileForClass(String className) {
className = className.replace('.', '/') + ".class";
URL classURL = ClassLoader.getSystemClassLoader().getResource(className);
if (classURL == null)
return null;
System.out.println("Class file URL: " + classURL);
// Adapt this if you also have '.war' or other archive types
if (classURL.toString().contains(".jar!")) {
String jarFileName = classURL.getPath().replaceFirst("!.*", "");
System.out.println("Containing JAR file: " + jarFileName);
try {
return new File(new URL(jarFileName).toURI());
}
catch (URISyntaxException | MalformedURLException e) {
throw new RuntimeException(e);
}
}
if (classURL.getProtocol().equals("jrt")) {
String jrtModule = classURL.getFile().replaceFirst("/([^/]+).*", "$1");
System.out.println("Target class is part of Java runtime module " + jrtModule);
String jmodName = System.getProperty("java.home") + "/jmods/" + jrtModule + ".jmod";
System.out.println("Containing Java module file: " + jmodName);
return new File(jmodName);
}
try {
return new File(classURL.toURI());
}
catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
When I call this from one of my IntelliJ IDEA projects with JDK 14 installed under a path containing spaces and deliberately adding a JAR also containing a path with spaces for testing, this code...
public static void main(String[] args) throws IOException {
Instrumentation instrumentation = ByteBuddyAgent.install();
instrumentation.appendToSystemClassLoaderSearch(
new JarFile("C:/Program Files/JetBrains/IntelliJ IDEA 2018.3/lib/idea_rt.jar")
);
Stream
.of(
Weaver.class.getName(),
String.class.getName(),
ByteBuddy.class.getName(),
"com.intellij.execution.TestDiscoveryListener"
)
.forEach(className -> System.out.printf("Found file: %s%n%n", getFileForClass(className)));
}
... yields this console output:
Class file URL: file:/C:/Users/alexa/Documents/java-src/Sarek/sarek-aspect/target/classes/dev/sarek/agent/aspect/Weaver.class
Found file: C:\Users\alexa\Documents\java-src\Sarek\sarek-aspect\target\classes\dev\sarek\agent\aspect\Weaver.class
Class file URL: jrt:/java.base/java/lang/String.class
Target class is part of Java runtime module java.base
Containing Java module file: C:\Program Files\Java\jdk-14.0.1/jmods/java.base.jmod
Found file: C:\Program Files\Java\jdk-14.0.1\jmods\java.base.jmod
Class file URL: jar:file:/C:/Users/alexa/.m2/repository/net/bytebuddy/byte-buddy/1.10.13/byte-buddy-1.10.13.jar!/net/bytebuddy/ByteBuddy.class
Containing JAR file: file:/C:/Users/alexa/.m2/repository/net/bytebuddy/byte-buddy/1.10.13/byte-buddy-1.10.13.jar
Found file: C:\Users\alexa\.m2\repository\net\bytebuddy\byte-buddy\1.10.13\byte-buddy-1.10.13.jar
Class file URL: jar:file:/C:/Program%20Files/JetBrains/IntelliJ%20IDEA%202018.3/lib/idea_rt.jar!/com/intellij/execution/TestDiscoveryListener.class
Containing JAR file: file:/C:/Program%20Files/JetBrains/IntelliJ%20IDEA%202018.3/lib/idea_rt.jar
Found file: C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar
You could do something like this:
final String bootClassPath;
final String[] entries;
// different VM may use a different string here...
bootClassPath = System.getProperty("sun.boot.class.path");
entries = bootClassPath.split(File.pathSeparator);
for(final String entry : entries)
{
System.out.println(entry);
}
Which goes through each entry in the boot classpath. For each entry you could then get the JAR file and look at the manifest file and see if it is yours. You could add another entry in the manifest to look for if there isn't already something unique to find.

Categories