• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classes and Packages  RSS feed

 
Bob Sherry
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following two files in a directory called java on my windows machine. The first file is:

package cn;

public class con1 {
public static final int K1 = 3;
};
~
The second file is:

import cn.*;

class use {
public static void main(String [] args )
{
system.out.println( "constand is " + con1.K1 );
}
}

The first file compiles with no problems. When I compile the second file,
I get the error message:
use1.java:6: cannot access con1
bad class file: .\con1.class
class file contains wrong class: cn.con1
Please remove or make sure it appears in the correct subdirectory of the classpath.
system.out.println( "constand is " + con1.K1 );
^
1 error

I am thinking that I need to put the first file in a subdirectory and build it there. I am hoping that somebody can help me out here.

Thanks
Bob
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two different ways to do this. You can, indeed, put the source files in a directory tree that mirrors the package structure, and ultimately, that's what you have to do on large projects. On small projects, you can get away with putting all the classes in one directory, but explaining to the compiler where to put the files. Use the "-d ." switch to tell the compiler to build that tree of directories right here:

javac -d . con1.java
javac -d . use1.java
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use1.java:6: cannot access con1
bad class file: .\con1.class
class file contains wrong class: cn.con1

For your programs to compile correctly, the con1.class file needs to be in a subdirectory cn (that's what the 'package cn;' statement promises);
That subdirectory must be at the end of the classpath used by the javac command.
When the compiler looked in the class file con1.class which was not in the subdirectory cn, it complained that the package.class name did not match its location (the current directory, noted by a . in the classpath.

Several solutions, as per the previous post where you specify the target directory for the compiler as an option,
or by building a directory structure that matches your package layout for your source.

However you do it, the class files must be in subdirectories that match the package's they are in.

It took me a long time before I got over having problems with Classpath.
[ July 03, 2008: Message edited by: Norm Radder ]
 
Bob Sherry
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses gentlemen. However, it is still not working for
me. I tried:

javac -d . con1.java
javac -d . use1.java

However, the second compilation came up with the same error. I also tried moving it to a sub directory. However, that did not work. I am thinking that I need to move it to a bin directory or something. Not sure. Do I need to set CLASSPATH? Right now, I am not setting CLASSPATH.

Thanks
Bob
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move con1 to the subfolder defined by its package statement.
cd to that folder and Compile con1 in that folder.
cd up one level to the folder containing the use program.
compile use in that folder.

The use program expects the con1 program to be in the folder defined by con1's package statement. The import statement in the use program tells the compiler where to look for missing bits like the con1 program.

To understand the relationship between classpath and package path, consider the full path to a class file that is in a package. If you write out the full path to the class file, the left hand part of it will be the classpath, and the righthand part will be the package path. For example if con1.class is located at: C:\javawork\home\cn\con1.class
Then the classpath is: C:\javawork\home\ and the package path is cn.con1
 
Bob Sherry
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to thank the group for their responses. However, part of the problem (if not the entire problem ) was how I spelled system. I should
have written:
System.out.println
but I wrote:
system.out.println

Case matters. Thanks again for the help in this matter.

Bob
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!