Reading Classes found in a Jar packaged in a Zipfile - java

I’ve spent a lot of time working on an assignment, “read a classfile found in a Jar stored contained in a Zipfile” and always return to the same ClassNotFoundException. I’m just seeking some guidance regarding how to proceed. I read many useful links on various sites, but continue to encounter problems either because a daunting task or I lack the knowledge.
Basically, my code successfully reads reads the Zipfile, creates a JarInputstream and finds my requested classfile. My problem, locating the file when the code executes the line, cl.loadClass(className) throwing the ClassNotFoundException.
URL jarUrl = new URL("jar:file:/E:/temp/ZipTest/JarTest_08262014/JarTest3.zip!/activation.jar!/com/sun/activation/viewers/ImageViewer.class");
URLClassLoader cl = URLClassLoader.newInstance(new URL[] {jarUrl });
className= "com/sun/activation/viewers/ImageViewer.class";
Class loadedClass = cl.loadClass(className);
Once I've loaded the class I can use the reflection methods to read the classfile. I wrote a class which works when reading jar files located in a directory.
I looked at OpenJDK javap code, but I don’t see any reflection calls. So, I’m lost at this point. Did I construct an incorrect URL?
Any suggestions?

I think you use the URLClassloader wrong.
Try to use a URL which points only to the JAR file.
Nested Jars are not allowed by default URLClassloader.
Further:
Don't specify the class's file.
Means:
className= "com/sun/activation/viewers/ImageViewer.class";
should be:
className= "com.sun.activation.viewers.ImageViewer";

Related

IllegalArgumentException at Java Jar resource access

I have spent all last night (until 3am) and this morning researching, testing, refactoring, and attempting to debug this issue. I have a simple Java game in Netbeans and while it runs perfectly perfect within the IDE in either run or debug mode, once exported into a jar file it refuses to load any resources corrrectly. There are many similar questions to this such as this one regarding loading an ImageIcon and despite great effort none of these solutions work for my project. I am not using ImageIcons, only simple BufferedImages and wav sound files. I recently refactored to combine my BufferedImageLoader and Sound classes into one Resource class, which I then moved into the same package as all my resources even though it worked perfectly well in a separate code package before in the IDE, although it works in its new location as well, strictly within the IDE.
I'm rather irritated and flustered from this issue. The truly infuriating thing is that this project used to work with resources after being exported into a jar, and now it seems to have stopped working with no changes. The only real programmatic difference between back when it worked and now is that I didn't have or use sound files back then, but this error isn't related to the sound files, as it catches an exception (and generates an error dialog) just from first trying to load the art assets.
I've tried every possible solution I've found in my research to no avail. Hopefully a fresh set of eyes can reveal the error of my ways.
The offending line of code is
return ImageIO.read(Resource.class.getResource("/res/" + imageFileName));
whereas imageFileName is the parameter with values passed from method calls such as
blockSheet = Resource.loadImage("art_assets/platform.png");
The location of the Resource class seemed to have no bearing on this working within Netbeans. My res folder is inside src, next to the com class package beginning.
It throws an IllegalArgumentException: input == null! exception. After some testing it seems that Resource.class.getResource("/res/" + imageFileName) returns a null value, which makes no sense at all. Again, this works perfectly perfect within the IDE. I can change the jar file into a zip and look inside to see that all the resources are exactly where they should be with the correct names and the correct extensions.
Here is a zip file of my entire project. Any help is immensely appreciated. Thank you.
EDIT:
Some of the things I've already tried:
getResourceAsStream() instead of getResource()
classLoader() between Resource.class and getResource()
this.getClass() instead of Resource.class from a non-static context
I think this should help:
How to get the path of a running JAR file?
CodeSource codeSource = YourMainClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
provided by Benny Neugebauer in the post.

Class loading by bootstrapclassloader

Here is what I was asked in interview:
If a new method is added to String class and compiled and put in
rt.jar, then will bootstrap class loader load it?
I answered that it will not get loaded but could not tell why.
Please help me with correct answer and explanation of that.
It will be loaded.
And not just that: it need not even be in rt.jar, if you prepend a jar file to the boot-classpath (see -Xbootclasspath/p:path), then you can even load java.lang.* classes from other jar files.
This of course is a way to violate the security of the JVM, but you need pretty deep access (either write-access to rt.jar or access to the command line parameters of the JVM) and when you have those, you can do a lot more than just replace String.toString().

Why does getResourceAsStream() work in the IDE but not the JAR?

I just want to read a file into my program. The file is located one directory above the working directory at "../f.fsh". So the following code runs correctly when I run it in the IDE
String name="../f.fsh";
InputStream is = getClass().getResourceAsStream(name);
InputStreamReader isreader=new InputStreamReader(is);//CRASHES HERE WITH NULL POINTER EXCEPTION
BufferedReader br = new BufferedReader(isreader);
but when I create a JAR file that has f.fsh zipped inside of it and run it, it crashes when creating the InputStreamReader, because the InputStream is null.
I've read a bunch of answers to questions about input streams and JAR files, and what I got out of it is that I should be using relative paths, but I am already doing that. From what I understand getResourceAsStream() can find files relative to the root of the project, that is what I want. Why does it not work in the JAR? What is going wrong, how can I fix it?
Does it have to do with the classpath? I thought that was only for including files external to the jar being run.
I have also tried, but still fail, when putting a slash in:
InputStream is = getClass().getResourceAsStream("\\"+name);
I looked at: How to get a path to a resource in a Java JAR file andfound that contents of a JAR may not necesarily be accesible as a file. So I tried it with copying the file relative to the jar (one directory up from the jar), and that still fails. In any case I'd like to leave my files in the jar and be able to read them there. I don't know what's going wrong.
You can't use .. with Class.getResourceAsStream().
To load a resource f.fsh in the same package as the class, use SomeClass.class.getResourceAsStream("f.fsh")
To load a resource f.fsh in a sub-package foo.bar of the package of the class, use SomeClass.class.getResourceAsStream("foo/bar/f.fsh")
To load a resource f.fsh in any package com.company.foo.bar, use SomeClass.class.getResourceAsStream("/com/company/foo/bar/f.fsh")
This is described in the javadoc of the getResource() method, although it lacks examples.
If .. works in Class.getResourceAsStream() while running from Eclipse, it's a bug in Eclipse. Eclipse and other IDEs implement custom class loaders to fetch resources from the project at runtime. It looks like the class loader implementation in Eclipse isn't performing all the necessary validations on input to getResourceAsStream() method. In this case the bug is in your favor, but you will still need to rethink how you structure your resources for your code to work in all cases.
it's mandatory that the name of the file is CASE SENSITIVE
it's mandatory to refresh (F5) the project explorer if the file is moved or copied outside Exclipse

ClassNotFoundException occured when replace a jar in runtime

I run a java application packaged in A.jar, in which some classes in B.jar are used.
All related jars are placed in a specific directory, which is included in the classpath.
The program is like this:
main(){
run method ClassA.M1() in A.jar; //the method may keep running for 2 minutes
do some other prepare;
call method ClassB.M2 in B.jar;
}
When the program is running M1, I manually replaced B.jar with a newer version(the name is also B.jar).
But, the program throw ClassNotFoundException.
Then, start the program again, and it works fine.
so, my question is: why the ClassNotFoundException is thrown, as the jar path and jar name is not changed, the classloader should load it without any troubles.
Give me some directions please.
You cannot simply change jar files during runtime by replacing them with ones having the same name, because the class loader might have already loaded some classes from it.
If you need to support such behavior, you need to look into libraries or frameworks that would provide hot replace mechanisms.
Here are two articles to help you understand class loaders better:
The Basics of Java Class Loaders
(1996)
Understanding the Java ClassLoader
(2001)
There are plenty more articles on this subject and even related questions here on the Stack OverFlow, I recommend you read more.

Does a .class file on disk have to follow the same directory structure as its qualified name in Java for us to run it?

After reading about dynamic class loading (that is, loading a .class file from disk) I am getting a bit worried.
Let's say I have a file called MyClass.class that contains the class a.b.c.MyClass.
Assuming I now decide to move the file to C:\(my root folder in Windows), I'd like to dynamically load this class. Is that possible, at all? From what I understood it seems MyClass' path would always have to be of the form *a/b/c.MyClass.
Thus, the following bit of code doesn't seem to work:
URL[] urls = new URL[] { new File("C:\\").toURL() };
URLClassLoader classLoader = new URLClassLoader(urls);
Class<?> targetClass = classLoader.loadClass("a.b.c.MyClass");
Forcing us to put a .class file in a directory structure that reflects its full internal name is insane, IMO. Am I missing something?
A possible implication of this fact would be that if I decide to copy a couple of .class files into a temporary directory so I can perform some awesome wizardry over them, I'll have to replicate all their dirty paths in that same temporary directory, which is awkward, at best.
Does a .class file on disk have to follow the same directory structure as its qualified name in Java for us to run it?
Yes, if you are using the standard classloaders.
In theory, you could implement a custom classloader that used a different scheme for locating the class files. But there's a good chance that you would run into problems when (for example) debugging your code. So I wouldn't recommend it.

Categories

Resources