0

I am using dynamic execution but it tells me that the class is not found although I double checked the path and it's correct

This is the method I am using

public static void runIt(String fileToCompile) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, SecurityException, NoSuchMethodException
        {System.out.println("Entered runIt()");
            String r2="";
File file=File("/Users/apple/Documents/Documents/workspace/UserTesting/src/two.java");
            System.out.println("The path of the file is "+fileToCompile);
            System.out.println("HERE 1");
            try
            {
                // Convert File to a URL
                URL url = file.toURL(); // file:/classes/demo
                URL[] urls = new URL[] { url };
                System.out.println("HERE 2");
                // Create a new class loader with the directory
                ClassLoader loader = new URLClassLoader(urls);
                System.out.println("HERE 3");
                System.out.println("HERE 4");
                Class<?> thisClass=null;
                try{
                thisClass = classLoader.loadClass("two");
                }
                catch(ClassNotFoundException e){
                    System.out.println("Class not found");
                }
                System.out.println("HERE 5");
                Object newClassAInstance = thisClass.newInstance();
                System.out.println("HERE 6");
                
                Class params[] = new Class[1];
                params[0]=String[].class;
                Object paramsObj[] = {};
                String m=null;
                Object instance = thisClass.newInstance();
                System.out.println("HERE 7");
                Method thisMethod = thisClass.getDeclaredMethod("main", params);
                System.out.println("HERE 8");
                String methodParameter = "a quick brown fox";
                // run the testAdd() method on the instance:
                System.out.println((String)thisMethod.invoke(instance,(Object)m));
               
                                  
            }
            catch (MalformedURLException e)
            {
            }
            
        }
 

This prints

HERE 1

HERE 2

HERE 3

HERE 4

Class not found

HERE 5

Is there anything missing in the method

Community
  • 1
  • 1
student1234
  • 201
  • 1
  • 8

2 Answers2

0

You load a .java file by trying to load a class. First you must compile your .java into a .class and only then load it. You can compile .java file dynamically via the Java Compiler API.

If you already have the source at compile time (you do not generate at run-time the content of two.java) you should call Class.forName("two"); instead of classLoader.loadClass("two").

Random42
  • 8,989
  • 6
  • 55
  • 86
  • I did the compilation using JavaCompiler and it is working fine .. Is the problem related to compilation ? – student1234 Jun 16 '13 at 18:00
  • When trying to load a class with `classLoader.loadClass(url)` you need to pass the URL of a class file (that is a file containing bytecode) not to a java file (that is file containing Java code). What you do is providing directly the uncompiled file to the classloader. – Random42 Jun 16 '13 at 18:04
  • so do you mean I should do this ? thisClass = classLoader.loadClass("two.class"); ? – student1234 Jun 16 '13 at 18:05
  • And what does it say? Are you sure that class is compiled? Do you have `two.class` file before running `runIt()`.? – Random42 Jun 16 '13 at 18:17
  • Do you have `two` class on the classpath when running the class calling `runIt()`? What error message does it say when calling `Class.forName()`? – Random42 Jun 16 '13 at 18:24
  • No error message cause I am running a plugin .. it just doesn't wokr – student1234 Jun 16 '13 at 18:28
0

You are passing .java file, read about the class loaders , it is used for loading classes not java files. First compile the class, than try loading it.(Once compiled you dont have to pass path as

/Users/apple/Documents/Documents/workspace/UserTesting/src/two.class

rather pass

/Users/apple/Documents/Documents/workspace/UserTesting/src/

This should work.(assuming your class in compiled and present in above mentioned dir)

ajay.patel
  • 1,957
  • 12
  • 15
  • When I pass File file=new File(/Users/apple/Documents/Documents/workspace/UserTesting/src/ ); and thisClass = classLoader.loadClass("two.class"); or thisClass = classLoader.loadClass("two"); , it's not working either – student1234 Jun 16 '13 at 18:09
  • Use class.forName as others suggested. Now around using this method. Set the classpath to point to this .class file if you are using terminal. If you are using eclipse, set the class path folder to the desktop folder. I am sure it will start working. Note: Make sure that you are using Class.forName("two"); – ajay.patel Jun 16 '13 at 18:27
  • what to do you mean by set the class path folder to the desktop folder – student1234 Jun 16 '13 at 18:42
  • export CLASSPATH=$CLASSPATH:/Users/zerocool/Desktop/two.class – ajay.patel Jun 16 '13 at 18:44
  • Above solution is when u are using terminal. for eclipse, right click on the project Properties->Java Build Path ->Libraries->Add external Class Folder. Here you give the path to your desktop where .class file is present. – ajay.patel Jun 16 '13 at 18:48
  • I am sorry for wasting your time but I cannot understand what you mean by export CLASSPATH=$CLASSPATH:/Users/zerocool/Desktop/two.class – student1234 Jun 16 '13 at 18:48