0

I read files from disc in my Java project. I find my hohoho.java file on D:/ and it's in File format, then I would like to add it as a class to existing package with my main class (Up.java). It should look like this - package -> Up.java, Hohoho.java.

And it should be done programmatically of course. I will use this .java file and it's functions in my Up.java.

Do you know any easy way to do this?

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;

public class Up{

public static void main(String[] args) {

    File root = new File("..\\.");
    File myfile = null;

    try {

        String[] extensions = {"java"};
        boolean recursive = true;

        Collection files = FileUtils.listFiles(root, extensions, recursive);

        for (Iterator iterator = files.iterator(); iterator.hasNext();) {
            File file = (File) iterator.next();
            String path = file.getAbsolutePath();
            System.out.println("File = " + path);

            String[] tokens = path.split("\\\\");

            for (String t : tokens)
              if (t.equals("Hohoho.java")){
                  myfile = file;
              }
            }

        System.out.println("Client class: " + myfile.getAbsolutePath());

    } catch (Exception e) {
        e.printStackTrace();
    }     

   }
}
6
  • 2
    yeah this can be done programatically but would be more convenient if you elaborate your scenario Commented Nov 15, 2012 at 11:47
  • 2
    Can you submit a snipe of code so that we can complete it for you? Commented Nov 15, 2012 at 11:47
  • 1
    If it's a java file, you have to call the compiler. If it's a class file, it's a little easier but still involved. Commented Nov 15, 2012 at 11:47
  • 1
    btw you can use file writer classes to make file and write the class accordingly and also there is a Compiler class which will compile it fro you too , but it seems like it would make it very complex , so again , elaborate your scenario Commented Nov 15, 2012 at 11:48
  • 1
    docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html Commented Nov 15, 2012 at 11:49

2 Answers 2

1

If you only need to load a .class file, per your comment, you should be able to just use something like the following (assuming no security issues in your setup):

    String path = "/path/to/your/classfiles/root/"; //not including the package structure
    ClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + path)}, Up.class.getClassLoader());

    Class clazz = loader.loadClass("foo.Hohoho"); //assuming a package "foo" for that class
    Object loadable = clazz.newInstance();
    Field field = loadable.getClass().getField("someField");  //or whatever - (this assumes you have someField on foo.Hohoho)

    System.out.println(field.get(loadable));

(I dropped all exception handling in the above). As @stemm pointed out, this is going to be painful to work with, just using pure Reflection.

Also, a quick test of invoking the java compiler, in the least complicated way possible, from within the VM follows. So, if case you do need to go from source, you can build off this:

    String sourcePath = "/path/to/your/javafiles/root/"; 
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int success = compiler.run(null, null, null,  sourcePath + "foo/Loadable.java");//can't get "-sourcepath","path" to go, for some reason.

    System.out.println("success = " + success); //should be 0; Sys err should have details, otherwise
Sign up to request clarification or add additional context in comments.

Comments

1

Use Java Compiler API to compile source code into bytecode.

After that use ClassLoader to load compiled class into jvm and you're be able to execute methods of that class.

If you sure, that compiled class implements specific interface - you can cast it to target interface and call methods directly, otherwise - you're need to use Reflection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.