title: How to run String which content code in java
body: <p><strong>Problem</strong></p>
  
  <p>I'm trying to run a java program which is contained in a string but it gives me a runtime exception i.e. <code>ClassNotFoundException</code> for <code>Test</code> class.</p>
  
  <p><strong>Error</strong></p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p><strong>Code</strong></p>
  
  <pre><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&lt;? extends JavaFileObject&gt; fileObjects;
  fileObjects = getJavaSourceFromString(program);
  
  compiler.getTask(null, null, null, null, null, fileObjects).call();
  
  Class&lt;?&gt; 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&lt;JavaSourceFromString&gt; getJavaSourceFromString(String code) {
  final JavaSourceFromString jsfs;
  jsfs = new JavaSourceFromString("code", code);
  return new Iterable&lt;JavaSourceFromString&gt;() {
    public Iterator&lt;JavaSourceFromString&gt; iterator() {
      return new Iterator&lt;JavaSourceFromString&gt;() {
        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();
        }
      };
    }
  };
  </code></pre>
  
  <p>Another class:</p>
  
  <pre><code>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;
      } 
  }
  </code></pre>
  
  <p>How can solve this problem, please help me? </p>
  
  <p>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 <code>Test</code> class?   </p>
  
  <blockquote>
    <p>/Code.java:1: error: ';' expected
    class Test{   public static void main (String [] args){      System.out.println >("Hello, World")      System.out.println (args.length);   }}</p>
  </blockquote>
  
  <p>And if i am try to execute from command prompt it will working correctly..then whats the problem with IDE i am using Eclipse?</p>
  
  <p><a href="https://i.stack.imgur.com/vY6iC.jpg" rel="nofollow noreferrer">hear is image of eclipse</a> .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 
  <a href="https://i.stack.imgur.com/cYUCX.jpg" rel="nofollow noreferrer">here is working directory image</a>  </p>