0

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

Last Update It worked now, after these commands, executed in the parent folder of `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
4
  • 1
    Well yes, you haven't declared a package called org.fedoraproject.helloworld.input. Look at your package statements: package org.fedoraproject.helloworld. You should also make your source locations match your package declarations, which they don't at the moment. Commented Jun 26, 2016 at 19:30
  • 1
    Mind you, the error message you're showing suggests your code isn't as described anyway: import input.Input - that's not the same as import org.fedoraproject.helloworld.input.Input. Commented Jun 26, 2016 at 19:31
  • @JonSkeet thanks for both hints. Commented Jun 26, 2016 at 19:54
  • I have replicated your project as you have described it, but I am not getting the error. Did you also recompile Input and Output after changing the package declaration? Commented Jun 26, 2016 at 20:51

2 Answers 2

2

1.) Change package declaration of the Input/Ouput class:

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

Since they are in the input/output folders.

2.) Classpath should be set to the root of all packages, and the main class passed should use the fully qualified name:

$ java -cp src org.fedoraproject.helloworld.HelloWorld
Sign up to request clarification or add additional context in comments.

9 Comments

well, after javac -cp src/ src/org/fedoraproject/helloworld/HelloWorld.java and java -cp src src/org/fedoraproject/helloworld/HelloWorl I get this: Error: Could not find or load main class src.org.fedoraproject.helloworld.HelloWorld
@toogley Could you try with ./src as the classpath or maybe src/ ? I usually do this on windows. But rest assured, the classpath should be set to the default package, i.e. the root.
@toogley Oh is just noticed, the src/ should not be in front of the path to the main class. Sorry.
oh, sorry. I didn't notice that.
Your java invocation is wrong - it should be java -cp src org.fedoraproject.helloworld.HelloWorld. You specify the name of a class, not its file system location.
|
0

the package header for input and output is not correct

you are doing

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

but input class is in

package org.fedoraproject.helloworld;

import java.util.Scanner;

public class Input {

and output

package org.fedoraproject.helloworld;

public class Output {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.