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.
Related
Say I have a String containing the content of a .java file. Are any APIs out there that would allow me to compile this source file into a virtual .class file (i.e. generate and store the content in memory, NOT creating an actual physical .class file on disk)? And this "virtual" .class would then be loaded and executed in the JVM?
Edit 1: The only reason I want to do this is because sometimes, my application might not have the write permission.
Use the JavaCompiler for this. I think the trick will be to define a custom JavaFileManager.
Java does have a compilation API to compile files dynamically, but I'm not aware of an option that would not persist the class files to disk. You can always use a ClassLoader and load those classes dynamically and then use them. You might be able to load the classes in memory by overriding the getFileForOutput method.
Optionally, this file manager might consider the sibling as a hint for
where to place the output. The exact semantics of this hint is
unspecified. The JDK compiler, javac, for example, will place class
files in the same directories as originating source files unless a
class file output directory is provided. To facilitate this behavior,
javac might provide the originating source file as sibling when
calling this method.
Another option is to use an Interpreter like BeanShell that will run the java code for you. It executes script like code and can work in repl mode.
Let's say I have a class file named ObjectFoo.class but I don't have access to ObjectFoo.java source file, since the class file is created during runtime. Is there a way to cast an object in this case ?
At the moment, I'm using this code to cast the object, but in this example I need to have ObjectFoo.java in my classpath :
Class c = Class.forName("ObjectFoo");
ObjectFoo object = (ObjectFoo) c.newInstance();
The goal is to instantiate an object from its class file using Java reflection.
I'm looking for a solution using only Java API and no other third-party libraries.
Thanks in advance for your answer !
Jonathan.
When you use third-party libraries, packages as .jar files, you will always compile and run against .class files. The .jar archives contain .class files, not .java ones.
So, the code above is correct, if the ObjectFoo class doesn't belong to a package. Otherwise, you will have to put the full package like path to the class.
To use a Java class, you do not need the source file. You only need the compiled class file, and you need to make sure that the Java compiler can find the class file at the right place.
Make sure that the directory that contains the file ObjectFoo.class is in the classpath when you compile and run your own code. You can do that by using the -cp option of the javac and java commands:
javac -cp C:\SomeDirectory;. MyProgram.java
java -cp C:\SomeDirectory;. MyProgram.java
where C:\SomeDirectory is the directory that contains ObjectFoo.class.
Note: There is also no reason to use reflection, with Class.forName(...) etc. to create a new instance of ObjectFoo. Just use the new operator instead; then you also don't need to cast.
ObjectFoo object = new ObjectFoo();
Good day,
I would like to know how I could import and call my library contained in a .jar file directly through RingoJS.
Thank you.
Edit:
Here is some additional informations. I have compiled a class named xsltProcessor that uses the SAXON library into a .JAR file. That class has a method named transform( xml file source, xslt file source ). That is the class and method I would like to use in RingoJS.
First, you'll have to add your library (the .jar file) to the classpath, either via Java's classpath or via adding it at runtime from within Ringo, using addToClasspath. See Ringo's Java integration wiki page for more detail.
Then, you can just use the classes contained within that library using Rhino's Packages.<java.package.name> and related features for scripting Java. Read Rhino's Scripting Java documentation for the details.
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 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.)