I have a problem I've been trying to solve for quite some hours. In an Eclipse plugin, I have an ArrayList that contains the full paths (as strings) of some java .class files. What I'd like to do is check if the classes that are included in the list extend a particular class. I thought about parsing the file, looking for what is after "extends," however we are talking about a .class file and not of a .java file. Any other ideas?
Thanks!
Use an URLClassLoader to load the file (Problem: You would need to know the right parent folder and Package + ClassName) and use the normal java.lang.Class methods to check.
The tool you're looking for is asm. (Or bcel, but asm is more popular these days.)
Related
I am using a Java external library, a .JAR file that contains a number of classes.
I Have two questions:
I have a problem when using classes in the .JAR file. The problem is when some variables is defined in the class itself, how can I access it? Does the class in the .JAR itself finds it automatically or I should call it?
I would like to know which is best to do: using an external library .JAR file or creating the classes and methods included in the .JAR file and include them in the project I am working on assuming that I have both the source code .JAVA files and the .JAR file of the classes I need to use?
Consider the code below, it is from an external project that I want to use in a current project, I have both .JAR and .JAVA files.
For example the code below has a variable named original_executer that is defined outside this method. If I call this method and give it the required string, will it do its function properly or an error will rise?
public boolean readSet(String setName){
testSet = testSetName;
OriginalLoader myLoader = new OriginalLoader();
original_executer = myLoader.loadTestClass(testSet);
original_obj = original_executer.newInstance();
if(original_obj==null){
System.out.println(" Can't instantiate original object");
return false;
}
return true
}
If you add the .jar to your classpath, you can use everything as if it was defined in your project.
If your .jar file is a external library it is best to keep the library in the .jar and use it from there. Whenever the library gets updated, you can just overwrite the libraries .jar.
given your jar is properly added in the classpath and you have used the necessary imports in your code you can use any class or variables with correct modifier of the jar...
best is to use the external library as jar..and to consume it through package dependency tool like Maven which will automatically download the latest version of jar for you. And then you can compile and run against the latest version
To access the variables defined in the class, you would need to use the getter methods that are supplied. Otherwise, you would need to employ Reflection to grab the values by doing something like
Class.getClass().getField("field_name").set(Class.getClass(), "value");
Although I'm not 100% sure on the validity of that, I'm sure it is something along those lines.
For the second question, I'm not quite sure what you're asking but you should always just add the .jar file to the classpath or if you want to modify the library, download the source code of it and put it into your workspace.
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 am programming in java using Eclipse, I am a novice and I'm stuck with the following problem: I have an Eclipse project that uses an external jar library. In this library there is a specific class that needs to be temporarily modified. I have the source code for this class. Is it possible to somehow include the source file for the code into the project, so that it will "override" the corresponding class in the jar file?
Thank you.
Basically, it's not possible to have two classes with the same signature (package + name) in the classpath but it's possible to import this class in your project in different package and use it instead of the original one.
Another way to solve this problem is to edit the .jar file by removing or changing the class that you need to be different.
However, note that changing an API is almost never a good idea.
I have a Java File # C:\Drive\MyFile.java
Now I want to read all the methods inside the java file.
I know about Class.forName() but how to pass local path inside the forName.
you would need to compile or at least parse the java file and look for methods. you will have better luck loading the compiled class file and using http://download.oracle.com/javase/6/docs/technotes/guides/reflection/index.html
There is nothing in java that can help with .java files, just .class file. Even then, you are not assured of successfully loading an arbitrary class file, since you will probably be missing a dependency.
If I had to do this, I would run javadoc with the -public, -package, -protected and -private on every .java file I found. This gives me an HTML file with a regular format that can easily be parsed. If I was more ambitious, I would write a custom javadoc doclet. If I was crazy ambitious, I would use antlr, yacc, javacc or another parser generator with a Java grammar to parse the Java files directly.
Try using reflection, which allows you to find variables, methods, constructors, etc. defined in a Java class.
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.