java 包会影响运行时行为吗?
如果我在abc包中定义了一个java类,但我只是将编译后的类文件放在c:\ ,并且使用URLClassloader来加载它,会出现错误吗?
编辑--------------------------------------------------------- ------------
package amarsoft.rcp.base.util.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class JavaCompolierDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String source = " package a.b.c; public class Test { public static void main(String args[]) { System.out.println(\"hello\"); } }";
// Save source in .java file.
File root = new File("C:\\java\\");
root.mkdir();
File sourceFile = new File(root, "\\Test.java");
Writer writer = new FileWriter(sourceFile);
writer.write(source);
writer.close();
// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
}
}
当我运行上面的代码时,定义的包 abc 不会生成为目录 a\b\c,Test.class 将位于 c:\Test 中。班级
if I define a java class in package a.b.c, but I just put the compiled class file in c:\
, and using a URLClassloader to load it, will there be errors ?
Edit----------------------------------------------------------
package amarsoft.rcp.base.util.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class JavaCompolierDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String source = " package a.b.c; public class Test { public static void main(String args[]) { System.out.println(\"hello\"); } }";
// Save source in .java file.
File root = new File("C:\\java\\");
root.mkdir();
File sourceFile = new File(root, "\\Test.java");
Writer writer = new FileWriter(sourceFile);
writer.write(source);
writer.close();
// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
}
}
when I run above code, the defined package a.b.c will not be generated as directory a\b\c, the Test.class will be in c:\Test.class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短:是
较长:您确实需要将工作目录设置为文件夹
a
所在的文件夹
(其中包含
b
)。否则它会按照您的要求给出错误。编辑后我的编辑:
您需要自己创建包,当然是通过创建文件夹。并将您要编译的源文件放入该包中。
如果您尝试编译不在正确包中的 Java 源代码,它将无法工作并且您应该会收到编译错误。我必须承认我从来没有这样编译过源代码。
Short: Yes
Longer: You really need to set the working directory to the folder where the folder
a
(which contains
b
) is in. Otherwise it will give errors as you asked.My Edit after your Edit:
You need to create the packages yourself, by creating folders of course. And put in that package the source file you want to compile.
If you try to compile Java source code, which isn't in the correct package, it won't work and you should get a compilation error. I have to admit that I never compiled source code this way.
如您所知,当您尝试加载
abc
时,它会在目录a/b
中查找文件c
,因此如果该文件不存在,它不会找到它。如果您更改 URLClassLoader 以在另一个目录中查找文件,它可以加载该类而不会出现错误。
你想做什么?
编辑:这是我几年前编写的一个小型编译器库的示例。它从内存中的 String 获取源代码并为您提供编译后的外部类。
这使用编译器 API 来执行此操作。如果您想调试代码,它会将其写入您指定的目录(以便您可以单步执行生成的代码),否则它完全在内存中工作。
As you know when you try to load
a.b.c
it will looking a directorya/b
for a filec
so if the file is not there, it won't find it.If you change the URLClassLoader to look for the file in another directory, it can load the class without error.
What are you trying to do?
EDIT: Here is an example for a small compiler library I wrote a few years ago. It takes the source from a String in memory and gives you the outer class compiled.
http://essence.svn.sourceforge.net/viewvc/essence/trunk/essence-file/src/test/java/org/freshvanilla/compile/CompilerTest.java?revision=293&view=markup
This uses the Compiler API to do this. If you want to debug the code it will write it to a directory you specify (so you can step through the generated code) otherwise it works entirely in memory.