I am given an external folder called "ATM".
In the folder, it only contains files that ends with .class.
How would I load it?
I've been using Class.forName. It does not work.
I need to load it to use Java's reflection on it.
You have basically two options:
make sure the folder is in the class path when you start the program
create an java.net.URLClassLoader to load the classes
Related
I'm planning to dynamically execute a .class files/bytecode. I'm trying to achieve this by first creating an unpacker which loads the .class files and jumps to the main function in the .class file and executes it while the unpacker shuts down.
How do I achieve this using Java?
If you are using ZIP and if the classes (packages) are placed in its root, then it is not even needed to unpack it. You can use your archive directly:
java -classpath my.zip mypackage.MyClass
If you want to launch one Java code from another Java application, either use Runtime.exec(), which is simple but not quite reliable (you may need to know location of JRE or particular path in the system PATH), or, more reliable but also harder to implement, use class loader, let it load unpacked classes (you mentioned decryption, so classloader should also decrypt classes before loading them), select needed class via this class loader, then invoke the main method.
You could just write a single main class that calls other classes based on the arguments passed to it.
In my project we are referencing lot of dependency .jar files.
/lib/xxx.jar
/lib/abc.jar
The xxx.jar file having some (com.search.hit) packages. The same packages are available in abc.jar file.But the problem comes into picture now, where accessing xxx.jar file it doesn't referencing their package(com.search.hit) instead it is referencing abc.jar package.
Could anyone tell how to redirect the flow?
The class from whichever jar comes first on the classpath is the one that is used. The other one might not even exist as far as the classloader is concerned. It's a good idea to avoid conflicts like this.
In Eclipse go to your project build path configuration and click on the Libraries tab.
Then remove the package that you don't want to be accessed from your list and add it again.
This will cause the package to be lower in the priority list and it'll check the other package before the one you just re-added.
This will create problems for you. My suggestion would be to create 2 extra classes for writing getter and setter wrappers for these jar files. Write 2 seperate classes, each one of them will reference just one of them and your project file will use these wrapper classes to invoke functions from these jars. It would be a lot easier that way.
You have to change the package name it can not be same file name with same package in single JVM. JVM will take load randomly one jar class using class loader and ignore the rest.
I would like to know if there is a posibility to instance a class of an external file
For example:
Oreja oreja_object = getClassFromExternalFile("C:\oreja_file.java");
Thanks,
Cristina.
Cristina, you can load external .class files dynamically using a custom ClassLoader, such as URLClassLoader. Have a look at this question.
You cannot load a .java source file directly. It must be compiled first, for example using the Java Compiler API.
I am using Matlab with Java integration and am able to link Java JAR files, enabling me to both create Java objects and call methods from within Matlab. The problem is with resources. For example, if I have an XML file located in the root of the JAR I can load it successfully from within a Java app, but not from Matlab.
Does anyone have an idea on how to make Matlab access resources within JAR files?
Thanks.
If you add a jar file to MATLAB's dynamic java classpath (e.g. using javaaddpath) the resources contained in the jar will NOT be visible to any java code. As discussed in this post, items on MATLAB's dynamic classpath are not really on the classpath of the JVM. In order to access the resources in my jar file I resorted to adding the jar to the static classpath. This can be accomplished in MATLAB R2012b by creating a file named javaclasspath.txt in the directory you start MATLAB that contains a list of the jar files you are using. Relevant MATLAB documentation
The normal Java Class.getResource methods should work when called from Matlab. What syntax are you using to try to load the resource in Java and in Matlab? If using getResource, are you calling it on a Class that was loaded from the same JAR that holds the resource?
In Matlab, classes on the static and dynamic classpaths use different ClassLoaders. Resources in a JAR on the dynamic classpath may not be visible to classes on the static classpath. So if your JAR is on the dynamic classpath, you need to make sure getResource is invoked on a class that came from that same ClassLoader.
I have a program that lets a user select any .class or .jar file and run it. The problem is that, to run it, I need to use something other than a java.io.File, which is what a JFileChooser returns. How can I make a java.io.File into a java.lang.Class or java.util.jar.JarFile?
If you have a File object of the class you want to load, you can use a URLClassLoader to load the class. The File object can provide the URL.
The problem is you need infer the classpath from a class itself. This isn't the easiest thing to do.
Your best bet is to parse the file using a tool such as asm.objectweb.org, and find the package of the class and infer the classpath root from the combination of the class's package and the filename. Even then, you are assuming that this class only uses classes in this one files directory.