The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
May 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

You originally used bad design

Posted by Ivan Porty on May 26, 2000 at 5:54 AM

> Hi,
> I have small program which has a base class (class name: "base"), with a defualt constructor wich just prints base's member variable. The scope of this class and the constructor is "Public". I create this in a package "MyPack" and save in a file called base.java.
> I have a different file called derived.java which imports the package "MyPack" and it derives from class base. In this class i just print the value of base class value.
> I have a third file called "demo.java" which has the main method creating an object of the derived class.
> Now i store these 3 files in a directory c:\test\*.java
> I want to compile these 3 files and put the resulting .class files in the directory c:\test\MyPack.
> The problem i'm facing is : the first file base.java gets compiled and puts the base.class file in
> c:\test\MyPack\base.class. Now while compiling derived.java, it gives a error that the "base class is not found".

> i compiled like :c:\test\> javac -d c:\test\MyPack\ derived.java

> I tried with -classpath option also. It's still giving the same error. How to compile this and what could be reason for the error?

Hello,

The reason for the error is that you originally used badly designed technique.
You shouldn't have put source files from packages not in their folders.
Because of that the compiler may be confused with SOURCE files, trying
to compile them, and it might not notice that class files are present in
correct folders. In other words, root dir source files has higher
priority than packaged class files. Remember that!

Guideline:
1. Create source files according to their packages. For example, if
you have as a root c:\test, and Java source file with "package myPack;",
then it should be saved as c:\test\myPack\JavaFile.java !
2. Create some Java file in root dir, such as your demo, that calles
classes form other packages.
2. After all call from c:\test
javac *.java OR javac demo.java
All classes, needed for demo and located in different packages, will be
compiled automaticly. No need for -d or -classpath

By the way, you have bizarre style of coding. Classes should be
started with uppercase letter, and packages should be all-lowercase

Hope you understand something from here.




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us