I'm trying to understand this demonstration example. Only the class HelloWorld
is given, therefore I have to implement the Input
and the Output
class myself.
i understand the error message: java can't find the Input.java
and the Output.java
file, while importing them. therefore the file HelloWorld.class
is not correctly built. But i don't understand the reason why this happens. I guess, I've made a small mistake in the directory structure of the filesystem or the imports - but i can't spot it. Where is my error?
I've read also 2 and 3, but that also doesn't work.
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.
Update:
After changing the package declarations of Input.java
and Output.java
to:
package org.fedoraproject.helloworld.input;
package org.fedoraproject.helloworld.output;
which produces (applying the suggestions in the answers):
$ 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
Aucun commentaire:
Enregistrer un commentaire