java 包会影响运行时行为吗?

发布于 2024-12-08 22:19:12 字数 1265 浏览 0 评论 0原文

如果我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ左 2024-12-15 22:19:13

简短:

较长:您确实需要将工作目录设置为文件夹 a
所在的文件夹
(其中包含b)。否则它会按照您的要求给出错误。


编辑后我的编辑:
您需要自己创建包,当然是通过创建文件夹。并将您要编译的源文件放入该包中。

 File sourceFile = new File(root, "a\\b\\c\\Test.java");
 sourceFile.getParent().mkdirs();
 FileWriter fw = new FileWriter(sourceFile);
 fw.write(source);
 fw.flush();
 fw.close();

 ....

如果您尝试编译不在正确包中的 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.

 File sourceFile = new File(root, "a\\b\\c\\Test.java");
 sourceFile.getParent().mkdirs();
 FileWriter fw = new FileWriter(sourceFile);
 fw.write(source);
 fw.flush();
 fw.close();

 ....

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.

黯然 2024-12-15 22:19:13

如您所知,当您尝试加载 abc 时,它会在目录 a/b 中查找文件 c,因此如果该文件不存在,它不会找到它。

如果您更改 URLClassLoader 以在另一个目录中查找文件,它可以加载该类而不会出现错误。

你想做什么?

编辑:这是我几年前编写的一个小型编译器库的示例。它从内存中的 String 获取源代码并为您提供编译后的外部类。

这使用编译器 API 来执行此操作。如果您想调试代码,它会将其写入您指定的目录(以便您可以单步执行生成的代码),否则它完全在内存中工作。

As you know when you try to load a.b.c it will looking a directory a/b for a file c 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文