简体   繁体   English

java.class文件的目录结构与源文件相同吗?

[英]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.

相关问题 IntelliJ IDEA:如何使从源文件(.java)编译的Java字节码文件(.class)与源文件位于同一目录中? - IntelliJ IDEA: how to make the java bytecode files (.class) compiled from the source files (.java) be in the same directory as the source files? 为什么Java源文件会进入目录结构? - Why do Java source files go into a directory structure? Java:访问与源文件位于同一目录中的文件 - Java: Accessing files that are in the same directory as your source file maven 目录结构的生成源文件的位置 - Location of generated source files for maven directory structure 两个java文件,在同一目录中,一个访问其他类/ s? - Two java files, in same directory, one accessing the others class/s? 用Java管理源文件和类文件 - Managing Source and Class Files in Java Maven资源文件,将java class文件和属性文件放在同一目录下 - Maven resource files, put java class files and property files in same directory 当前目录中的java类文件? - java class files in current directory? 我可以从命令访问javac和java,但不能访问同一目录中的.class文件和.java文件 - I can access javac and java from command, but not .class files and .java files in the same directory 如何在任何文件夹中执行java.class? - How to excute java.class in any folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM