java程序不识别从属类"包输入不存在"

时间:2016-06-26 19:26:24

标签: java compiler-errors

我试图理解this demonstration example。只提供了课程HelloWorld,因此我必须自己实施InputOutput课程。

我理解错误消息:java在导入时无法找到Input.javaOutput.java文件。因此,文件HelloWorld.class未正确构建。但我不明白为什么会这样。我想,我在文件系统或导入的目录结构中犯了一个小错误 - 但我无法发现它。我的错误在哪里?

我还阅读23,但这也不起作用。

HelloWorld.java

package org.fedoraproject.helloworld;

import org.fedoraproject.helloworld.input.Input;
import org.fedoraproject.helloworld.output.Output;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.print("What is your name?: ");
        String reply = Input.getInput();
        Output.output(reply);
    }
}

Input.java

package org.fedoraproject.helloworld;

import java.util.Scanner;

public class Input {

    public static String getInput() {
        Scanner scanner = new Scanner(System.in);
        String returnVal = scanner.next();
        scanner.close();

        return returnVal;
    }

}

Output.java

package org.fedoraproject.helloworld;

public class Output {

    public static void output(String s) {
        System.out.println(s);

    }
}
$ find
.
./src
./src/org
./src/org/fedoraproject
./src/org/fedoraproject/helloworld
./src/org/fedoraproject/helloworld/output
./src/org/fedoraproject/helloworld/output/Output.class
./src/org/fedoraproject/helloworld/output/Output.java
./src/org/fedoraproject/helloworld/input
./src/org/fedoraproject/helloworld/input/Input.class
./src/org/fedoraproject/helloworld/input/Input.java
./src/org/fedoraproject/helloworld/HelloWorld.class
./src/org/fedoraproject/helloworld/HelloWorld.java
$ java -cp src/org/fedoraproject/helloworld/input/Input.class:src/org/fedoraproject/helloworld/output/Output.class src/org/fedoraproject/helloworld/HelloWorld.class
Error: Could not find or load main class src.org.fedoraproject.helloworld.HelloWorld.class
$ javac -cp src/ src/org/fedoraproject/helloworld/HelloWorld.java
src/org/fedoraproject/helloworld/HelloWorld.java:3: error: cannot access Input
import org.fedoraproject.helloworld.input.Input;
                                         ^
  bad source file: src/org/fedoraproject/helloworld/input/Input.java
    file does not contain class org.fedoraproject.helloworld.input.Input
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.

更新

Input.javaOutput.java的包声明更改为:

package org.fedoraproject.helloworld.input;
package org.fedoraproject.helloworld.output;

产生(应用答案中的建议):

$ javac -cp src org/fedoraproject/helloworld/HelloWorld.java
org/fedoraproject/helloworld/HelloWorld.java:3: error: package org.fedoraproject.helloworld.input does not exist
import org.fedoraproject.helloworld.input.Input;
                                         ^
org/fedoraproject/helloworld/HelloWorld.java:4: error: package org.fedoraproject.helloworld.output does not exist
import org.fedoraproject.helloworld.output.Output;
                                          ^
org/fedoraproject/helloworld/HelloWorld.java:9: error: cannot find symbol
        String reply = Input.getInput();
                       ^
  symbol:   variable Input
  location: class HelloWorld
org/fedoraproject/helloworld/HelloWorld.java:10: error: cannot find symbol
        Output.output(reply);
        ^
  symbol:   variable Output
  location: class HelloWorld
4 errors

上次更新 现在,在这些命令之后,它在`src':

的父文件夹中执行
$ find -type f
./src/org/fedoraproject/helloworld/output/Output.java
./src/org/fedoraproject/helloworld/output/Output.class
./src/org/fedoraproject/helloworld/input/Input.class
./src/org/fedoraproject/helloworld/input/Input.java
./src/org/fedoraproject/helloworld/HelloWorld.java
./src/org/fedoraproject/helloworld/HelloWorld.class
~/java-example-project 
$ javac -cp src/ src/org/fedoraproject/helloworld/HelloWorld.java
~/java-example-project 
$ java -cp src org.fedoraproject.helloworld.HelloWorld
What is your name?: toogley
toogley

2 个答案:

答案 0 :(得分:1)

1。)更改Input / Ouput类的包声明:

package org.fedoraproject.helloworld.input;
package org.fedoraproject.helloworld.output;

因为它们位于input / output个文件夹中。

2。)Classpath应该设置为所有包的根,并且传递的主类应该使用完全限定名

$ java -cp src org.fedoraproject.helloworld.HelloWorld

答案 1 :(得分:0)

输入和输出的包标头不正确

你正在做什么

import org.fedoraproject.helloworld.input.Input; import org.fedoraproject.helloworld.output.Output;

但输入类在

package org.fedoraproject.helloworld;

import java.util.Scanner;

public class Input {

和输出

package org.fedoraproject.helloworld;

public class Output {