0

I have a program in which I studied and tried out the Observer Pattern.

I wrote it with nano on my terminal, and I saved it like this:

kneipe
    (location
       (Show.java))
    (leute
       (Beobachter.java
       Comedian.java))

I compiled correctly all of them but it keeps saying that in my main the references to the classes aren't right so it can't make new Objects from the other classes.

Here is the deal:
I tried it out on eclipse and it works fine with import package2.*;

And all of the tutorials I found online or even in books are only with one package-folder.

So, how do I organise my project and how do I import correctly because I tried (almost) everything and nothing worked so far (I even tried copying the second package into the first, which didn't work either).

ClassA:

package leute;

import java.util.*;

public class Comedian extends Observable {
    String name = "";
    int numGags = 0;
    String joke = "abc";
    boolean toldjoke = false;

    public Comedian(String name, int numGags) {
        this.name = name;
        this.numGags = numGags;
    }

    public void telljoke() {
        this.toldjoke = true;
        System.out.println(joke);
        setChanged();
        notifyObservers();
    }

    private String getname() {
        return this.name;
    }

    private int getnumgags() {
        return this.numGags;
    }
    }

Thats ClassB:

package leute;

import java.util.*;

public class Besucher implements Observer {
    String name = "";
    int intus = 0;
    boolean lacht = false; 

    public Besucher(String name, int intus) {
        this.name = name;
        this.intus = intus;
    }

    private String getname() {
        return this.name;
    }

    public void enought(Besucher a) {
        a.lacht = false; 
    }

    public void update(Observable o, Object arg) {
        lacht = true;
        System.out.println("HAHAHAH!!!");
    }
}

Thats the Main:

package location;

import leute.*;
import java.util.*;

public class Show {

    public static void main(String args[]) {
        Observer franz = new Besucher("Franz", 6);
        Observer karl = new Besucher("Karl", 1);
        Comedian mark = new Comedian("Mark", 5);

        mark.addObserver(franz);

        mark.telljoke();


        mark.addObserver(karl);

        mark.telljoke();

        mark.deleteObserver(karl);
        mark.deleteObserver(franz);

        mark.telljoke();
    }
}

As mentioned it works fine on eclipse but the terminal tells me this:

lisaundamelia@lisaundamelia ~/Schreibtisch/Alle Projekte $ javac
  kneipe/location/Show.java
kneipe/location/Show.java:3: error: package leute does not exist
import leute.Besucher;
            ^
kneipe/location/Show.java:4: error: package leute does not exist
import leute.Comedian;
            ^
kneipe/location/Show.java:10: error: cannot find symbol
        Observer franz = new Besucher("Franz", 6);
                             ^
  symbol:   class Besucher
  location: class Show
kneipe/location/Show.java:11: error: cannot find symbol
        Observer karl = new Besucher("Karl", 1);
                            ^
  symbol:   class Besucher
  location: class Show
kneipe/location/Show.java:12: error: cannot find symbol
        Comedian mark = new Comedian("Mark", 5);
        ^
  symbol:   class Comedian
  location: class Show
kneipe/location/Show.java:12: error: cannot find symbol
        Comedian mark = new Comedian("Mark", 5);
                            ^
  symbol:   class Comedian
  location: class Show
6 errors
LAA
  • 3
  • 4
  • You need to tell us **what** tells you **which precise message**. Don't paraphrase it. Post the exact and complete message, and the relevant code it refers to. – JB Nizet Jul 02 '16 at 22:22
  • Please provide some code, especially the package line and the class definition. Moreover, the exact error message would be helpful. – Jan B. Jul 02 '16 at 22:25

3 Answers3

1

You need to compile all your source files at once. Or at least specify the classpath so that the compiler knows where to find the classes that Show depends on:

javac -cp kneipe kneipe/location/Show.java

By default, the current directory is in the classpath, so you can also move to the keipe directory, and just do

javac location/Show.java

I strongly suggest to always use the -d option to specify where the compiler should put the .class files, in a separate directory from your source files.

And in fact, once you get accustomed to how Java works, you'd better use a real build tool like gradle or Maven (but I much prefer gradle), that would make you adopt good layout conventions, and would compile, jar, etc. everything for you, with a single command.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

You should name the file according to the class.

ClassB.java should be Besucher.java for example.

If it works in eclipse, then the problem is likely how you're compiling.

Here is how I would do it:

javac **/*.java
cilki
  • 125
  • 1
  • 13
0

You didn't specify a classpath.

javac -cp kneipe kneipe/location/Show.java
Andreas
  • 154,647
  • 11
  • 152
  • 247