0

Problem

I'm trying to run a java program which is contained in a string but it gives me a runtime exception i.e. ClassNotFoundException for Test class.

Error

Exception in thread "main" java.lang.ClassNotFoundException: Test at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

Code

public class CompileString {
public static void main(String[] args) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String program = "class Test{" + "   public static void main (String [] args){"
    + "      System.out.println (\"Hello, World\");"
    + "      System.out.println (args.length);" + "   }" + "}";

Iterable<? extends JavaFileObject> fileObjects;
fileObjects = getJavaSourceFromString(program);

compiler.getTask(null, null, null, null, null, fileObjects).call();

Class<?> clazz = Class.forName("Test");
Method m = clazz.getMethod("main", new Class[] { String[].class });
Object[] _args = new Object[] { new String[0] };
m.invoke(null, _args);
}

static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
final JavaSourceFromString jsfs;
jsfs = new JavaSourceFromString("code", code);
return new Iterable<JavaSourceFromString>() {
  public Iterator<JavaSourceFromString> iterator() {
    return new Iterator<JavaSourceFromString>() {
      boolean isNext = true;

      public boolean hasNext() {
        return isNext;
      }

      public JavaSourceFromString next() {
        if (!isNext)
          throw new NoSuchElementException();
        isNext = false;
        return jsfs;
      }

      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }
};

Another class:

class JavaSourceFromString extends SimpleJavaFileObject {
final String code;

    JavaSourceFromString(String name, String code) {
        super(URI.create("string:///" + name.replace('.', '/') + 
        Kind.SOURCE.extension), Kind.SOURCE);
        this.code = code;
    }

    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
        return code;
    } 
}

How can solve this problem, please help me?

Here is the error if i try to change any code in the String which gives me compile error as well means everything working right then why it not found the Test class?

/Code.java:1: error: ';' expected class Test{ public static void main (String [] args){ System.out.println >("Hello, World") System.out.println (args.length); }}

And if i am try to execute from command prompt it will working correctly..then whats the problem with IDE i am using Eclipse?

hear is image of eclipse .class file create at run time but why i can't access that is there any other way please tell me. i am not able to access the Test.class because it created the outside of the project it required to create in same folder that other .class file is created here is working directory image

Amol Raje
  • 928
  • 3
  • 9
  • 16
  • maybe you should have a look at this Thread https://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class – Michael Meyer Oct 11 '17 at 12:01
  • Michael Meyer..thanks for reply may be i need to store the file locally ... – Amol Raje Oct 11 '17 at 13:18
  • if i am try to execute from command prompt it will execute right..then whats the problem with IDE i am using Eclipse – Amol Raje Oct 11 '17 at 14:16

1 Answers1

0

The first thing I'd do is place the Class.forName("Test") call in a try{ } catch(ClassNotFoundException cnfe) {} block to handle the potential exception.

Next, you might want to take a look at the example provided here, compiling Java programmes from memory

In particular you should check to see if the call() method on your compiler is returning true. If not then the Test class is never created and hence won't be found when you try to call it.

Space Cadet
  • 385
  • 6
  • 23
  • Space Cadet.. Boolean result=compiler.getTask(null, null, null, null, null, fileObjects).call(); it return the true – Amol Raje Oct 11 '17 at 12:22
  • means Test class is created – Amol Raje Oct 11 '17 at 12:23
  • also if try to change any code in the string which gives me an compile error as well means everything is working right then why it not found the class Test? – Amol Raje Oct 11 '17 at 13:16
  • Space Cadet.."here, compiling Java programmes from memory" i try this example as well which will give the same error that class is not found ...is there any problem with java version i currently using 1.8 v – Amol Raje Oct 11 '17 at 14:02
  • if i am try to execute from command prompt it will execute right..then whats the problem with IDE i am using Eclipse? – Amol Raje Oct 11 '17 at 14:17