Javac compilation error, problem with packages...

S

Salil P

Hi,
Sorry for a slightly long post...

I have a (probably trivial?) problem with compilation of java sources
that I can reproduce with the simple example here.

There are two classes that I need to compile.

Files: With source dir as E:\base\src
E:\base\src\one.java
E:\base\src\twopkg\two.java

Javac output dir: E:\base\cls

Source code:

one.java
----------------------
public class one
{
int doThis() { return 10; }
}
----------------------


E:\base\src\twopkg\two.java
----------------------
package twopkg;

public class two
{
int doThat() { return 20; }

one returnOne() { return new one(); }
}
----------------------


Compilation gives errors:

E:\base>javac -sourcepath src -d cls src\one.java src\twopkg\two.java
src\twopkg\two.java:7: cannot resolve symbol
symbol : class one
location: class twopkg.two
one returnOne() { return new one(); }
^
src\twopkg\two.java:7: cannot resolve symbol
symbol : class one
location: class twopkg.two
one returnOne() { return new one(); }
^
2 errors
----------------------

Since "one" class is not in a package, "import one;" in two.java gives
a compilation saying something like "Expecting a . instead of ;"

It seems that I am not able to refer a class with no package from a
class within a package. Is this right? / Am I doing something wrong?

Also, I understand it is bad form to have classes without packages, but
I am writing a code generation tool and need to support this possible
scenario...

Thanks in advance,
Salil P
 
J

John C. Bollinger

Salil said:
There are two classes that I need to compile.

Files: With source dir as E:\base\src
E:\base\src\one.java
E:\base\src\twopkg\two.java

Since Java 1.<mumble>, classes in named packages cannot reference
classes in default (unnamed) packages. In your particular case, class
"one" is in a default package and class "two" is in a package named
"twopkg", so the latter cannot reference the former. Put class "one" in
a named package.


John Bollinger
(e-mail address removed)
 
A

Andrew Thompson

Sorry for a slightly long post...

Do not worry. A 67 line post hardly counts as 'slightly long'.

It would require 100-150 lines (of new material) before most
people would even begin to consider a post to be 'slightly long'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top