Javac如何处理多个文件,目录,类和源? [英] How does Javac work for multiple files, directories, classes and source?

查看:226
本文介绍了Javac如何处理多个文件,目录,类和源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚javac如何处理sourcepath,classpath和prebuilt类等等。我正在尝试阅读文档,但无法理解它。

I'm trying to figure out how javac works with regard to stuff like sourcepath, classpath and prebuilt classes etc. I'm trying to read the documentation, but can't really make sense of it.

我试过想到下面的一些示例案例。

I've tried to think of some sample cases below.


  1. 如果我' m编译单个文件onlyfile.java没有依赖关系,有2个A和B类,A类使用B类,是否需要在A之前定义/声明B类?或者javac是否聪明且多次传递或类似的事情?

  1. If I'm compiling a single file onlyfile.java which has no dependencies, which has 2 classes A and B , and class A uses class B , does class B need to be defined/declared before A ? Or is javac smart and does multiple passes or something like that ?

root.java使用位于同一文件夹中的文件file2.java中的另一个类。如果我执行javac root.java,javac如何知道在文件夹中搜索类文件,如果没有找到,那么对于源文件呢?

root.java uses another class in a file file2.java located in the same folder. If I execute javac root.java , how does javac know to search the folder for the class file and if not found , for source file instead ?

如果file2位于子目录中,上述工作是什么?

How does the above work if the file2 is located in a subdirectory ?

编辑:
我读到某个导入的地方只是一种减少打字而不是加载像python中的任何东西的方法。
假设我只构建了一个使用多个其他类的java文件,并且这些类文件已经存在。没有导入,类对象的abcd部分已经告诉我在哪里搜索类文件,那么为什么有cp选项?

I read somewhere that import is just a way to cut down on typing rather than "loading" anything like in python. Suppose that I'm building only 1 java file which uses multiple other classes, and that these class files already exist. Without import, the a.b.c.d part of the class object already tells me where to search for the class file, then why a cp option ?

推荐答案

1)如果编译使用B类的A类,那么B类也将被强制使用。如果编译B类(在A中使用,但在B中未使用A),则不会强制使用A类。查找更多详细信息示例此处

1) If you compile class A which uses class B then class B will be compelled as well. If you compile class B (which is used inside A, but A is not used inside B), class A will not be compelled. Find more details end examples here.

2)javac在source-path和class-path中搜索。如果你运行没有参数的javac,比如 javac A.java ,它会将classpath和sourcepath设置为当前目录。如果在类路径或源路径中都找不到请求的类,则会出现编译错误。

2) javac searches inside source-path and class-path. If you run javac without arguments like javac A.java it sets classpath and sourcepath to current directory. If requested class is not found neither in classpath nor in sourcepath you'll have compilation error.

3)Java对项目结构有严格的规定。您不能简单地将源文件放在另一个文件夹中而不更新文件内容。

3) Java has strict rules for project structure. You can't simply place source file to another folder without updating file content.

项目中的每个文件夹都应具有包声明的文件夹层次结构。

Every folder in the project should have folder hierarchy with respect of package declaration.


定义:包是一组提供访问保护和名称空间管理的相关类型。

Definition: A package is a grouping of related types providing access protection and name space management.

例如,如果你有类 A.java 这样的包声明

for instance if you have class A.java with package declaration like this

package com.mycompany;

相应的文件夹结构应如下所示:

The corresponding folder structure should look like this:

com/mycompany/A.java

如果你遵循这个规则,编译器将能够解决依赖关系,就像我在#1中解释的那样。有关更多信息,请此处

If you follow this rules compiler will be able to resolve dependencies just like I explained in #1. Find more information here.

这篇关于Javac如何处理多个文件,目录,类和源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆