javac "cannot find symbol" error with command line
Asked Answered
P

4

12

I have two classes Owning and OwningAccessor. The files are in the same directory.

public class Owning {
    String _name = "";
    public void printBanner()
    {
    }
    public void printOwning(double amount)
    {
        printBanner();

        //print details
        System.out.println("name:" + _name);
        System.out.println("amount:" + amount);
    }
}


public class OwningAccessor {
    public void access()
    {
        Owning o = new Owning();
        o.printOwning(500);
    }
}

When I tried to compile OwningAccessor with javac -cp . OwningAccessor.java, I got compilation error.

symbol  : class Owning
location: class smcho.OwningAccessor
        Owning o = new Owning();
        ^
OwningAccessor.java:6: cannot find symbol
symbol  : class Owning
location: class smcho.OwningAccessor
        Owning o = new Owning();
                   ^

What's wrong with this? The code compiles fine under eclipse IDE.

Pouliot answered 15/11, 2012 at 23:21 Comment(7)
are they in the same directory? Show your source path directory structureScarf
They are in the same directory.Pouliot
No repro, works here. Does it work if you first compile Owning.java?Brennabrennan
Don't you need a this in there somewhere?Penurious
@Daniel: No, it doesn't work even I compile the Owning.java to get class file.Pouliot
And what if you remove the -cp .? Wouldn't be needed for files in the current directory anyway.Brennabrennan
@Daniel : Same error without -cp ..Pouliot
Q
19

Ok, let's suppose you have the code distributed in files as follows

myproject
├── out
└── src
    ├── OwningAccessor.java
    └── Owning.java

Go to your command prompt, and change directory to myproject. Once there issue the following command:

javac -d out -sourcepath src src/OwningAccessor.java

I just tested it and it works just fine. Your compiled classes will be located in the out folder:

.
├── out
│   ├── OwningAccessor.class
│   └── Owning.class
└── src
    ├── OwningAccessor.java
    └── Owning.java

Compiling one class will trigger the compilation of all other dependent classes. The compiler will automatically look for them in the src folder.

Quent answered 15/11, 2012 at 23:58 Comment(2)
this just complies the program, i wonder why I am not getting output of systme.out.print in CMD ?Vineyard
Best way is to create a jar it will take care of all this and is fairly simple.Sup
G
14

Make sure you compile both Owning.java and OwningAccessor.java, like so:

javac -cp . Owning.java OwningAccessor.java

Eclipse compiles all necessary files for you, which is why does work there.

Gimcrack answered 15/11, 2012 at 23:23 Comment(5)
Doesn't java figure out dependencies on its own?Scarf
It does if you use the -sourcepath flag in your compilerQuent
Sourcepath should do the trick, unfortunately I cannot test it as javac on my machine crashes with an ACCESS_VIOLATION on anything I try to compile...Gimcrack
@Kninnug: -sourcepath doesn't work. I don't see reason why compilation all the source at once works whereas one by one does not.Pouliot
Well, like I said, I can't test it right now (except in Eclipse, which is what we don't want). But apparently javac doesn't look for the necessary files by itself. You could also enter javac -cp . *.java which should compile all the .java files in that directory.Gimcrack
T
1

Try to make a correct sourcepath example:

javac -d temp -sourcepath c:\awork\JavaProjects\singleton\src\ c:\JavaProjects\singleton\src\com\company\MySingleton.java

javac -d temp -sourcepath c:\awork\JavaProjects\singleton\src\ c:\JavaProjects\singleton\src\com\company\Main.java

In "temp" we alocate resources and with -sourcepath indicate where are the .java files.

Twirl answered 29/4, 2015 at 20:37 Comment(0)
A
0

So, in a directory named D:\Automation there is a file Demo.java throwing this error, in cmd while you are in D:\Automation, you need to : - 1) cd.. //will pull you out from Automation directory. In D:> 2) javac Automation\Demo.java

this will compile your file - Demo.java

Abhorrence answered 8/8, 2018 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.