[英]java.class files directory structure same as source files?
Currently i do some runitime compilation from a src file to a jar with following steps: 目前,我通过以下步骤从src文件到jar执行一些运行时编译:
First: generate source files in a dir and subdirs 第一:在目录和子目录中生成源文件
/main
/submain1
/submain2
Second: Compile code (currently all in .class files go into this directory) 第二:编译代码(当前所有.class文件都进入该目录)
/builddir
Third: Generate jar from .class file into target folder 第三:从.class文件生成jar到目标文件夹
/target
compilation is done by Java ToolProviders Javacompiler 编译由Java ToolProviders Javacompiler完成
ArrayList<File> files1 = getSourceFiles();
Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files1);
JavaCompiler.CompilationTask task = compiler.getTask(null ,fileManager , null, optionList, null, compilationUnits1 );
boolean compiled = task.call();
Now my first question is, would it be better to separate all .class files into different compilation units and rebuild the same directory structure as the source files have? 现在我的第一个问题是,将所有.class文件分成不同的编译单元并重建与源文件相同的目录结构会更好吗?
And if so, how can it be done? 如果是这样,怎么办?
Java expects that foo.bar.MyClass
is found at foo/bar/MyClass.class
(with some additional classpath related requirements), otherwise you will get ClassNotFoundExceptions
(or is it NoClassDefFoundErrors
) up the wazoo. Java希望在foo/bar/MyClass.class
找到foo.bar.MyClass
(还有一些与类路径有关的其他要求),否则您将在wazoo上得到ClassNotFoundExceptions
(或者是NoClassDefFoundErrors
)。
So not only should you have the same structure, you must have the same structure. 因此,不仅应该具有相同的结构,而且必须具有相同的结构。 I don't know why you're compiling like that instead of using an existing build tool which would generate correct results by default. 我不知道为什么要像这样编译,而不是使用现有的生成工具,默认情况下会生成正确的结果。
Creating a jar file is equivalent to creating a zip file, so just as a successful compilation doesn't mean that your program works, neither does having a jar file mean it would be a working one. 创建一个jar文件等同于创建一个zip文件,因此就像成功的编译并不意味着您的程序可以工作一样,拥有jar文件也不意味着它可以正常工作。
Now my first question is, would it be better to separate all .class files into different compilation units and rebuild the same directory structure as the source files have? 现在我的第一个问题是,将所有.class文件分成不同的编译单元并重建与源文件相同的目录结构会更好吗?
The output structure of the .class files must mirror the package names used in your source files. .class文件的输出结构必须镜像源文件中使用的程序包名称 。 AFAIK folder structure of the source files does not matter that much. 源文件的AFAIK文件夹结构没有太大关系。
If you use any other structure in your .jar file, classes cannot be found by the class loader. 如果您在.jar文件中使用任何其他结构,则类加载器无法找到类。
So the answer to your question is no . 因此,您的问题的答案是否定的 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.