求解 java动态编译找不到类,往各位大虾指导
http://hi.baidu.com/shenshouer/blog/item/f6493cd9104a53f239012f9f.html?timeStamp=1315...
http://hi.baidu.com/shenshouer/blog/item/f6493cd9104a53f239012f9f.html?timeStamp=1315734921739
需求:
将一个类作为字符串利用java编译器编译成.class文件,然后再加载调用其方法。
但测试没通过,没策了。求助!
具体如下:
CompilerTest.java类:
package com.compiler;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
/**
* 动态编译Java 源文件
* @author shenshouer
*
*/
public class CompilerTest {
public static void main(String[] args) throws Exception {
String source =
"public class Main { " +
"public static void main(String[]args) {" +
"System.out.println(\"Hello World!\");" +
"} " +
"}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main", source);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
boolean result = task.call();
if (result) {
System.out.println("编译成功。");
ClassLoader loader = CompilerTest.class.getClassLoader();
try {
Class<?> clazz = loader.loadClass("Main");
Method method = clazz.getMethod("main", new Class<?>[] {});
Object value = method.invoke(null, new Object[] {});
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class StringSourceJavaObject extends SimpleJavaFileObject {
private String content = null;
public StringSourceJavaObject(String name, String content) throws URISyntaxException {
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
}
}
执行完后将字符串中的类编译成了class文件 但文件放在与源代码文件夹同级的目录下面?
执行到 Class<?> clazz = loader.loadClass("Main");
时 报加载不到类
求解
执行结果具体如下:
编译成功。
java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at com.compiler.CompilerTest.main(CompilerTest.java:43) 展开
需求:
将一个类作为字符串利用java编译器编译成.class文件,然后再加载调用其方法。
但测试没通过,没策了。求助!
具体如下:
CompilerTest.java类:
package com.compiler;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
/**
* 动态编译Java 源文件
* @author shenshouer
*
*/
public class CompilerTest {
public static void main(String[] args) throws Exception {
String source =
"public class Main { " +
"public static void main(String[]args) {" +
"System.out.println(\"Hello World!\");" +
"} " +
"}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main", source);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
boolean result = task.call();
if (result) {
System.out.println("编译成功。");
ClassLoader loader = CompilerTest.class.getClassLoader();
try {
Class<?> clazz = loader.loadClass("Main");
Method method = clazz.getMethod("main", new Class<?>[] {});
Object value = method.invoke(null, new Object[] {});
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class StringSourceJavaObject extends SimpleJavaFileObject {
private String content = null;
public StringSourceJavaObject(String name, String content) throws URISyntaxException {
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
}
}
执行完后将字符串中的类编译成了class文件 但文件放在与源代码文件夹同级的目录下面?
执行到 Class<?> clazz = loader.loadClass("Main");
时 报加载不到类
求解
执行结果具体如下:
编译成功。
java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at com.compiler.CompilerTest.main(CompilerTest.java:43) 展开
1个回答
展开全部
参照网上其他童鞋的例子与介绍,修改了一下:
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
/**
* 动态编译Java 源文件
*
* @author shenshouer
*
*/
public class CompilerTest {
public static void main(String[] args) throws Exception {
String source = "public class Main { " + "public static void main(String[]args) {"
+ "System.out.println(\"Hello World!\");" + "} " + "}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main",
source);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
// 获取编译类根路径,不然会报找不芦郑卖到类的错误
String path = Class.class.getClass().getResource("/"陪逗).getPath();
Iterable< String> options = Arrays.asList("-d", path);
// 增加options参数
// CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects);
boolean result = task.call();
if (result) {
System.out.println("编译成功。");
ClassLoader loader = CompilerTest.class.getClassLoader();
try {
Class<?> clazz = loader.loadClass("Main");
Method method = clazz.getMethod("main", String[].class);
// 修改调用参数,不然丛绝会报参数个数不对
// Object value = method.invoke(null, new Object[] {});
Object value = method.invoke(null, new Object[] {new String[]{}});
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class StringSourceJavaObject extends SimpleJavaFileObject {
private String content = null;
public StringSourceJavaObject(String name, String content) throws URISyntaxException {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
Kind.SOURCE);
this.content = content;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
}
}
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
/**
* 动态编译Java 源文件
*
* @author shenshouer
*
*/
public class CompilerTest {
public static void main(String[] args) throws Exception {
String source = "public class Main { " + "public static void main(String[]args) {"
+ "System.out.println(\"Hello World!\");" + "} " + "}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main",
source);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
// 获取编译类根路径,不然会报找不芦郑卖到类的错误
String path = Class.class.getClass().getResource("/"陪逗).getPath();
Iterable< String> options = Arrays.asList("-d", path);
// 增加options参数
// CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects);
boolean result = task.call();
if (result) {
System.out.println("编译成功。");
ClassLoader loader = CompilerTest.class.getClassLoader();
try {
Class<?> clazz = loader.loadClass("Main");
Method method = clazz.getMethod("main", String[].class);
// 修改调用参数,不然丛绝会报参数个数不对
// Object value = method.invoke(null, new Object[] {});
Object value = method.invoke(null, new Object[] {new String[]{}});
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class StringSourceJavaObject extends SimpleJavaFileObject {
private String content = null;
public StringSourceJavaObject(String name, String content) throws URISyntaxException {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
Kind.SOURCE);
this.content = content;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询