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
Community
  • 1
  • 1
toogley
  • 551
  • 1
  • 4
  • 11
  • 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. – Jon Skeet Jun 26 '16 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`. – Jon Skeet Jun 26 '16 at 19:31
  • @JonSkeet thanks for both hints. – toogley Jun 26 '16 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? – Jorn Vernee Jun 26 '16 at 20:51

2 Answers2

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
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • 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 Jun 26 '16 at 19:35
  • @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.](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html) – Jorn Vernee Jun 26 '16 at 19:39
  • @toogley Oh is just noticed, the ```src/``` should not be in front of the path to the main class. Sorry. – Jorn Vernee Jun 26 '16 at 19:46
  • oh, sorry. I didn't notice that. – toogley Jun 26 '16 at 19:54
  • 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. – Jon Skeet Jun 26 '16 at 19:56
  • @JornVernee I addressed that in my updated question. – toogley Jun 26 '16 at 20:04
  • @JonSkeet Yes, but using ```/``` instead of ```.``` works too. (I just used the word 'path' because it looks like one) – Jorn Vernee Jun 26 '16 at 20:28
  • @JornVernee: It does now, but it didn't used to - and I don't know whether it works universally. It's definitely better to specify the class name, IMO. I would *definitely* make a point of using the class name in its regular form in answers. – Jon Skeet Jun 26 '16 at 20:29
  • @JonSkeet I usually use ```.``` but I thought ```/``` was the standard/original way. Good to know :) – Jorn Vernee Jun 26 '16 at 20:35
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 {
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97