• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
class1.java
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks! your instructions worked! yay!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for sharing this information, guys.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,
Welcome to the Ranch.

Did you notice the thread is more than a year old?
http://faq.javaranch.com/java/DontWakeTheZombies
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks!
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anna:

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.

John.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank a lot. I do not know why I have not thought of that..
 
reply
    Bookmark Topic Watch Topic
  • New Topic