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)
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?
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.
posted 9 years ago
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.
posted 9 years ago
Thank a lot. I do not know why I have not thought of that..