java程序不';t承认下级阶级”;“包输入不存在”;
我在努力理解。只给出了类java程序不';t承认下级阶级”;“包输入不存在”;,java,compiler-errors,Java,Compiler Errors,我在努力理解。只给出了类HelloWorld,因此我必须自己实现Input和Output类 我理解错误消息:java在导入时找不到Input.java和Output.java文件。因此,文件HelloWorld.class未正确生成。但我不明白为什么会这样。我想,我在文件系统或导入的目录结构上犯了一个小错误——但我无法发现它。我的错误在哪里 我也读过,但那也没用 HelloWorld.java package org.fedoraproject.helloworld; import org.
HelloWorld
,因此我必须自己实现Input
和Output
类
我理解错误消息:java在导入时找不到Input.java
和Output.java
文件。因此,文件HelloWorld.class
未正确生成。但我不明白为什么会这样。我想,我在文件系统或导入的目录结构上犯了一个小错误——但我无法发现它。我的错误在哪里
我也读过,但那也没用
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);
}
}
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;
}
}
package org.fedoraproject.helloworld;
public class Output {
public static void output(String s) {
System.out.println(s);
}
}
Input.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);
}
}
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;
}
}
package org.fedoraproject.helloworld;
public class Output {
public static void output(String s) {
System.out.println(s);
}
}
Output.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);
}
}
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;
}
}
package org.fedoraproject.helloworld;
public class Output {
public static void output(String s) {
System.out.println(s);
}
}
更新: 将
Input.java
和Output.java
的包声明更改为:
package org.fedoraproject.helloworld.input;
package org.fedoraproject.helloworld.output;
产生(应用答案中的建议):
上次更新 在执行了这些命令之后,它现在在“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.)更改输入
/输出
类的包声明:
package org.fedoraproject.helloworld.input;
因为它们位于输入
/输出
文件夹中
2.)类路径应设置为所有包的根,传递的主类应使用完全限定名:
1.)更改输入
/输出
类的包声明:
package org.fedoraproject.helloworld.input;
因为它们位于输入
/输出
文件夹中
2.)类路径应设置为所有包的根,传递的主类应使用完全限定名:
输入和输出的包标头不正确 你在干什么 导入org.fedoraproject.helloworld.input.input; 导入org.fedoraproject.helloworld.output.output 但是输入类在
package org.fedoraproject.helloworld;
import java.util.Scanner;
public class Input {
和输出
package org.fedoraproject.helloworld;
public class Output {
输入和输出的包标头不正确 你在干什么 导入org.fedoraproject.helloworld.input.input; 导入org.fedoraproject.helloworld.output.output 但是输入类在
package org.fedoraproject.helloworld;
import java.util.Scanner;
public class Input {
和输出
package org.fedoraproject.helloworld;
public class Output {
是的,您还没有声明名为
org.fedoraproject.helloworld.input
的包。查看您的包语句:package org.fedoraproject.helloworld
。您还应该使源位置与包声明相匹配,而目前它们并不匹配,您显示的错误消息表明您的代码与所描述的不一样:import-input.input
-这与importorg.fedoraproject.helloworld.input.input
@JonSkeet不同。谢谢这两个提示。我已经复制了您描述的项目,但我没有收到错误。在更改包声明后,您是否也重新编译了Input
和Output
?是的,您没有声明名为org.fedoraproject.helloworld.Input的包。查看您的包语句:package org.fedoraproject.helloworld
。您还应该使源位置与包声明相匹配,而目前它们并不匹配,您显示的错误消息表明您的代码与所描述的不一样:import-input.input
-这与importorg.fedoraproject.helloworld.input.input
@JonSkeet不同。谢谢这两个提示。我已经复制了您描述的项目,但我没有收到错误。在更改包声明后,您是否也重新编译了输入
和输出
,在javac-cp-src/src/org/fedoraproject/helloworld/helloworld.java
和java-cp-src/org/fedoraproject/HelloWorl/HelloWorl
之后,我发现:错误:找不到或加载主类src.org.fedoraproject.helloworld.helloworld
@toogley您可以尝试使用/src
作为类路径吗src/
?我通常在windows上这样做。但是请放心,@toogley Oh刚刚被注意到,src/
不应该位于主类的路径前面。对不起,哦,对不起。我没有注意到。您的java
调用是错误的-应该是java-cp src org.fedoraproject.helloworld.helloworld
。指定类的名称,而不是其文件系统位置,在javac-cp-src/src/org/fedoraproject/helloworld/helloworld.java
和java-cp-src/org/fedoraproject/HelloWorl/HelloWorl
之后,我发现:错误:找不到或加载主类src.org.fedoraproject.helloworld.helloworld
@toogley您可以尝试使用/src
作为类路径吗src/
?我通常在windows上这样做。但是请放心,@toogley Oh刚刚被注意到,src/
不应该位于主类的路径前面。对不起,哦,对不起。我没有注意到。您的java
调用是错误的-应该是java-cp src org.fedoraproject.helloworld.helloworld
。指定类的名称,而不是其文件系统位置。