How to load a java class outside the classpath? - java

I have a program where I want the user to be able to choose a .java class file from the file system, and then have that class loaded into the program.
I'm using a JFileChooser to allow the user to select a file. Then, I tried converting that file to a URL, and using a URLClassLoader to load the class (as suggested by these answers).
The problem is that, when I want to use the loadClass() method, I don't know the "full class name" of the class (e.g. java.lang.String). So, I don't know how to make this method work. Is there a way to get this class name? Or is there another way to do this?
Here is a sample of my code:
// Open the file chooser
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
File obtainedFile = fileChooser.getSelectedFile();
// Create the class loader from the file
URL classPath = obtainedFile.toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[] {classPath});
// Get the class from the loader
Class<?> theClassIWant = loader.loadClass("the file name"); // What do I put here??

Load a single class file is generally completely useless. Said class file isn't alone; it has more class files that are relevant. Even if you think 'nah, there is just one source file, do not worry about this', note that a single java file can easily generate multiple class files.
Thus, two options:
Don't load class files. Load jar files.
Use the usual mechanisms (META-INF/services or META-INF/MANIFEST.MF) to put some sort of class name in there so you know what to load. Then create a new classloader with the provided jar, load the manifest, figure out the main class, load that, and run it.
Attempt to determine the 'root' for the loaded class file and include that on the classpath.
This is quite difficult - the problem is, to 'load' a class file you need to tell the loader what the fully qualified name is of that class before it is loaded. But how do you know the fully qualified name? You can surmise the class name from the file (not quite always true, but usually), but the package is a more difficult issue.
You can open the class file yourself as a binary stream and write a basic class file format parser to get the fully qualified class name. Easy for an experienced java programmer. Quite tricky for someone new to java (which I gather you are, if you think this is a good idea).
You can also use existing tools to do this, such as bytebuddy or asm.
Finally, you can try a spaghetti-at-the-wall method: Keep travelling up the directory until it works. You know it isn't working if exceptions occur.
For example, to load C:\MyDir\Whatever\com\foo\MyApp.class, You first try creating a new classloader (see the API of URLClassLoader which is part of core java) using as root dir C:\MyDir\Whatever\com\foo, and then you ask it to load class MyApp.
If that works, great (but usually trying to load package-less classes is simply a non-starter, you're not supposed to do that, the CL API probably doesn't support it, intentionally, there is no fixing that).
If it doesn't, instead try C:\MyDir\Whatever\com, and load class foo.MyApp. If that doesn't work, try C:\MyDir\Whatever and load class com.foo.MyApp, and so on.
The considerable advantage is, if there is another class sitting right next to MyApp.class, and MyApp needs it, this will work fine.
You'll need to write a while loop (traversing the path structure using Paths.get and p.getParent()), catch the right exception, manipulate the path into the class name (using .replace and +), and, of course, create a class loader (URLClassLoader), load classes with it (invoke loadClass), and if you intend on running it, something like thatClass.getConstructor().newInstance() and then thatClass.getMethod("someMethod", String.class, /* all the other args here */).invoke(theInstanceYouJustMade, "param1", /*all other params */) to actually 'run' it, more to be found in the java.lang.reflect package.

Related

Class.forName doesn't find a loaded class

I am experimenting with some cheats made for a Java game. A specific cheat blew my mind. I noticed that all the names of the classes it loads start with an exclamation mark so I decided to try and get one of those classes by name. The cheat crashes if you forget to delete the settings file it creates so I got the name of one of them - "!M". I decided that I'd inject a jar file in the game myself and experiment with that class. I created a JAR loader in C and the JAR itself. I used Cheat Engine to verify that the class itself is loaded in the JVM. I created a simple JAR which called this code upon injection:
try {
Class<?> cheatClass = Class.forName("!M");
showMSGBox(cheatClass.getName());
showMSGBox(cheatClass.getSuperclass().getName());
} catch (Exception ex) {
showMSGBox(ex.toString());
}
Where showMSGBox(string) simply shows a JOptionPane. When I injected the JAR in the game using the loader I made, I was granted with an error which stated that there is no such a class. I opened Cheat Engine again and saw the class is still there. Any ideas why this could happen and what causes it?

NullPointer when getting resource as stream

I am trying to access a file from my project's directory, let's call it
src/main/project/foo/bar.txt
In class MyClass
But when I do:
InputStream is = MyClass.class.getResourceAsStream("bar.txt");
And do anything with the InputStream, e.g.
is.toString();
I get a NullPointerException
I have also tried:
MyClass.class.getResourceAsStream("/bar.txt");
and
MyClass.class.getResourceAsStream("./bar.txt");
Is there something extra I need to do as the file is not in the same package as the class in which I'm calling it? This application is deployed on TomCat, so absolute references to the filepath are impractical.
EDIT: To the person who flagged it as a duplicate of nullpointer, I know what a nullpointer is, obviously it can't find the resource. As to why that is the case, I need help figuring out.
if your class is at src/main/project/foo/MyClass.java MyClass.class.getResourceAsStream("bar.txt"); should be fine.
if your resource is in a different folder within the project than the class use:
MyClass.class.getClassLoader().getResource("main/project/foo/bar.txt");
in general:
MyClass.class.getClassLoader().getResource("package/<subpackage/>filename");
To my knowledge this works with any class anywhere within the same project.
MyClass.class.getResourceAsStream(); operates relative to the Location of myClass and you can't change that.

Java Class Loading Flow

can anyone give me an idea of how the class loading flow works?,
What i'm trying to say is that i need to understand what happend when a developer create a new object eg:
User user = new User();
where java goes to load the class, I try excluding a class from my build and setting the current thread class loader with a jar that contains the excluded class and run it with no good results.
ok, i found it. Thanks!.
Java looks for the classLoader of the class that contains it.

How to instantiate a class without knowing its package?

I have an Eclipse project (MainProject) and it references another Eclipse project (ReferencedProject). MainProject also references a JAR file (ReferencedJar). This ReferencedJar's file name is known. I also know there is a class (ReferencedClass) in ReferencedJar, but I don't know in what package ReferencedClass is because the package path is not known beforehand.
I need to instantiate ReferencedClass in ReferencedProject using reflection. How can I do this? And will the solution be okay when the project is packaged to a its standalone jar outside Eclipse?
The reason for this question is; The ReferencedJar is file generated by a modeller application. It generates java classes for your model and puts them into ReferencedJar. The user can choose which package the classes it generates will be put into. But the class names are always the same. MainProject is project that will include this generated jar, but ReferencedProject (a framework) also needs to instantiate a class in this generated jar. Hope this makes the question more clear.
Thanks in advance
Edit: Actually I have an idea but don't know how to implement it. Because I know the name of ReferencedJar file, I could access it on runtime and check all the classes it contains. Then I can find the matching class by name comparison. But how can I access the ReferencedJar on runtime?
As long as the class you need is in the class path, you can get a reference to it by invoking Class.forName(String className)
String className = "WhatEver";
String packageName = "some.package";
Class<?> c = Class.forName(packageName + "." + className);
If you don't know the package name, however, and there's no way to get it from a configuration file, I would recommend using a library like reflections to scan the class path and find the relevant class.
You can not instantiate a class you if do not know its package

Access classes from package

I'm developing an android test app and i'm going to access all internal class of android.view package. android.view is a package that is present in jar file. I tried by loading package name but it doesn't display the classes if any one tried
this already, please help.
Here's what I tried so far:
public static void main() throws ClassNotFoundException{
Class o =Class.forName("android.view");
Class[] C=o.getDeclaredClasses();
for(int i=0;i<C.length;i++) {
Classname = C[i].getName();
ClassesDisplayActivity.your_array_list3.add(Classname);
Log.i("Ramu","classname "+ C[i].getName());
}
}
}
It is not possible to determine at runtime all of the classes that are in a package using a standard class loader.
You might have some luck with this library though:
https://code.google.com/p/reflections/
Package is not a class. You cannot call Class.forName() for package and access classes that belong to class using getDelcaredClasses().
I do not know what do you really need, so I'd recommend you to explain this in separate question. probably you will receive better solutions.
However if you really need this you have to do the following:
Get your classpath by calling System.getProperty(java.class.path)
split this property to its elements by colon
iterate over the list and read each resource. If resource is jar you can use ZipInputStream, if it is a directory use File class.
filter list of resources you got at #3.
Fortunately you can use 3rd party library named Reflections that helps you to do all this without writing code.

Categories