【问题标题】:How to compile two java files in the same package [duplicate]如何在同一个包中编译两个java文件[重复]
【发布时间】:2015-11-15 16:17:35
【问题描述】:

这是我在一个文件中的代码:

package AI;

public class Class1 {
    public static void main1() {
        System.out.println("Hello");
    }
}

还有另一个文件:

package AI;

public class partsOfSpeech {
    public static void main(String args[]) {
        Class1 firstInstance = new Class1();
        firstInstance.main1();
    }
}

如何编译这些文件,以免出现找不到 Class1 的错误消息?编译后如何运行它们?

【问题讨论】:

  • 你是用ide还是命令行
  • 如果你使用命令行编译。首先编译 Class1 然后编译第二类 partsOfSpeech 。但是如果你使用 IDE 没有问题是自动构建的
  • 不,我这样做了,但仍然没有用
  • 当我尝试编译部分语音时,它说它找不到 Class1

标签: java compilation


【解决方案1】:

1) 转到 AI 上方的一个目录(例如 D:\Study\Examples\src

2) javac -d 。 AI/Class1.java

3) javac -d 。 AI/partsOfSpeech.java

4) java AI.partsOfSpeech

输出:

你好

目录结构:

在我的系统中,AI 目录在 src 目录下。

D:\Study\Examples\src\AI 目录

11/15/2015  11:06 PM    <DIR>          .
11/15/2015  11:06 PM    <DIR>          ..
11/15/2015  11:06 PM               390 Class1.class
11/15/2015  10:58 PM               118 Class1.java
11/15/2015  11:06 PM               333 partsOfSpeech.class
11/15/2015  10:59 PM               177 partsOfSpeech.java
               4 File(s)          1,018 bytes
               2 Dir(s)  236,298,690,560 bytes free

D:\Study\Examples\src>

【讨论】:

    【解决方案2】:

    只需在命令行中打开包 AI 并

    D:\AI>javac partsOfSpeech.java Class1.java
    

    两个类都编译--- 对于在 Commend 行中运行类

    D:/>java AI.partsOfSpeech 

    你的班级运行

    【讨论】:

    • 没有,它给了我:javac: file not found: AI/partsOfSpeech.java 用法:javac 使用 -help 获取可能的选项列表跨度>
    • 还是不行。
    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 2022-01-12
    • 2021-06-14
    • 2014-03-14
    • 2013-08-31
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多