gpt4 book ai didi

java程序无法识别从属类 "package input does not exist"

转载 作者:行者123 更新时间:2023-12-02 03:31:19 27 4
gpt4 key购买 nike

我试图理解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;
}

}
<小时/>

输出.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

最佳答案

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

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

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

2.) 类路径应设置为所有包的根目录,并且传递的主类应使用完全限定名称:

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

关于java程序无法识别从属类 "package input does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38042368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com