0

Can't find a solution to this problem.

Guitar2.java:

public class Guitar2
{
    private String serialNumber;

    public Guitar2(String serialNumber)
    {
        this.serialNumber = serialNumber;
    }

    public String getSerialNumber()
    {
        return serialNumber;
    }
}

Inv2.java:

import java.util.List;
import java.util.LinkedList;

public class Inv2
{
    private List guitars;

    public Inv2()
    {   
        guitars = new LinkedList();
    }

    public void addGuitar(String serialNumber)
    {
        Guitar2 guitar = new Guitar2(serialNumber);
        guitars.add(guitar);
    }
}

Both files are in the same directory, both are 755 and the directory is in the classpath. I get the error message:

[machine]me @ directory $ javac Inventory.java 
Inventory.java:18: cannot find symbol
symbol  : class Guitar
location: class Inventory
        Guitar guitar = new Guitar();
        ^
Inventory.java:18: cannot find symbol
symbol  : class Guitar
location: class Inventory
        Guitar guitar = new Guitar();
                            ^
2 errors

I read that if a file is in the same directory, classes from it can be used in other files in the same directory without any import statements. What's the problem here?

POST EDIT Output when using the above code:


[me]machine @ ricks $ javac Inv2.java 
Note: Inv2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I get the .class files of both .java files.

TMOTTM
  • 3,286
  • 6
  • 32
  • 63
  • Try doing `javac Guitar.java` first, then `javac Inventory.java`--does that solve it? (I'm not sure it should be necessary, but it's worth trying.) – ajb May 31 '14 at 00:02
  • Eclipse, Netbeans, Intellij... choose your IDE. Use your text-editor for something else ;-) – donfuxx May 31 '14 at 00:03
  • Well, it looks like you got past the first problem. The "unchecked" problem is because you're using `List` and `LinkedList` with no type parameter. Those should be avoided--Java allows them only for backward compatibility. Use `List` and `LinkedList`. – ajb Jun 01 '14 at 01:08

3 Answers3

3

Run javac Guitar.java and only then (after it has been compiled and a Guitar.class was created) run javac Inventory.java

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

Try to compile Guitar.java first. then run your cmd . or try this : javac *.java

Mifmif
  • 3,132
  • 18
  • 23
  • When doing `javac *.java`, I get `Note: Inventory.java uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.` – TMOTTM May 31 '14 at 00:08
  • see [this](http://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning) – Mifmif May 31 '14 at 00:14
  • @TMOTTM that's a warning - but it doesn't mean that your code didn't compile. Were the `.class` files created ? – Nir Alfasi May 31 '14 at 00:20
  • @Mifmif unless he's using very different sources than the trivial ones he posted, this link is not relevant. – ajb May 31 '14 at 00:24
  • @TMOTTM Are the really small sources in your question the actual ones you're compiling, or is there more stuff in there that you (understandably) left out of the original post? – ajb May 31 '14 at 00:25
  • i think that what is posted is not what is tested. – Mifmif May 31 '14 at 00:27
  • I edited the question and am now posting the exact source code. – TMOTTM May 31 '14 at 10:25
  • Ok, so it compiles and I found that the ArrayList is not securely instantiated, so that should fix the warning. But in any case, I should be able to use classes from different files (within the same directory) without having to use `import` statements. – TMOTTM May 31 '14 at 10:33
0

Make sure both classes have the same package. Even if they are in the same directory, a different package could cause this problem. Type the same package or use an import and then try compiling with javac *.java

Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48