wood burning stoves 2.0
The moose likes Beginning Java and the fly likes compiling a package with javac vs eclipse (cannot find symbol) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compiling a package with javac vs eclipse (cannot find symbol)" Watch "compiling a package with javac vs eclipse (cannot find symbol)" New topic

compiling a package with javac vs eclipse (cannot find symbol)

harold wang

Joined: Sep 09, 2008
Posts: 3
Hi *,

I'm new to java and I'm having some issues compiling under certain conditions. I'm working with sun's jre 1.6 (including javac), so I think the reliance on classpath is not so heavy, but you all can correct me if I'm wrong.

Here's the situation:

I have these files in my directory: ~/proj/src/myPackage
class2.java (has a main() in it and makes a reference to class1)

Both of these files begin with the line:
'package myPackage;'

I can build/debug this project in eclipse no problem. The problem comes when I try to invoke javac directly on class2.java. It gives me the 'cannot find symbol' for every reference to a variable of type class1 in class2.main() .

My Classpath is empty, and I've also tried compiling with:
javac -cp . class2.java
and it still doesn't work.

One thing that does seem to work is to simply remove the package statements from the top of class1.java and class2.java .

But... I want to declare these things in a package, so this is not a suitable workaround. i'd really like to know why this is happening.

(btw... I definitely have spelled the package name consistently, and I even tried changing my references to class1 to myPackage.class1 and even an import statement on that class- but every time javac still bombs with the cannot find symbol message)

Thanks! hw
marc weber

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

When using javac, what is your current directory?

Because these classes are part of the myPackage package, they have qualified names of myPackage.class1 and myPackage.class2. These are in the src directory (not the myPackage directory).

So if you are using a classpath of the current directory (either explicitly with the dot, or implicitly as the default when no classpath is specified), then the current directory needs to be src.

Then specify the file to compile relative to src...

...src>javac -cp . myPackage/class2.java
[ September 09, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

you need the fullpath name to compile classes in package.

So you need to be on ~proj/src directory and compile with the following command:

harold wang

Joined: Sep 09, 2008
Posts: 3
thanks! your instructions worked! yay!
David Chambers

Joined: Oct 14, 2009
Posts: 1
Thanks very much for sharing this information, guys.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44585
Welcome to the Ranch

Avoid using package names if you are just beginning. You will find more about compiling with package names if you use the search button above, or look at these old threads: 1 2 3
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10747

Welcome to the Ranch.

Did you notice the thread is more than a year old?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Anna Ka

Joined: Nov 04, 2009
Posts: 2
Hi everybody,

I had a problem similar to Harold's (could not use javac with a package), read the instructions and it all works now (Thanks a lot, by the way!). But I wonder where could I look for more info about how javac works with packages? I am starting a relatively long term project where I will need to compile and run code written in Eclipse with javac, lots of nested packages. I managed to get my one-package code working for now, but not so sure I actually understand why it works. I googled looking for a tutorial, of course, but I could not really find anything that would really explain how it works. I read lots before I found this post and finally it work.

if anyone know of such a pointer, could you please share it?

John de Michele

Joined: Mar 09, 2009
Posts: 600

Java expects that package names reflect the pathname from where the CLASSPATH points to. So, if you have a class in package com.somecompany.project.subproject.category, the path from where the CLASSPATH points to would be com/somecompany/project/subproject/category/Class.java.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44585
Welcome to JavaRanch ( ) Anna Ka.
Please search this forum for "compile package" and you will find quite a lot of old threads which will be helpful.
Anna Ka

Joined: Nov 04, 2009
Posts: 2
Thank a lot. I do not know why I have not thought of that..
I agree. Here's the link: http://aspose.com/file-tools
subject: compiling a package with javac vs eclipse (cannot find symbol)
It's not a secret anymore!