wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Packages - is this a reasonable way to deal with them? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packages - is this a reasonable way to deal with them?" Watch "Packages - is this a reasonable way to deal with them?" New topic
Author

Packages - is this a reasonable way to deal with them?

Rupert Brown
Greenhorn

Joined: Jul 29, 2009
Posts: 17
I've alway struggled with packages, but think that I've finally figured it out.

On a Windows machine, the package name has to match up to a directory structure. So if you want to put things in a package called suncertify.db, that relates to a directory structure called suncertify\db. But to make all this work, Java has to know where suncertify is, and that is where the CLASSPATH variable comes in.

Assume I want to create the files in my suncertify.db package in a complete directory path called:

C:\Rupert\myJavaFiles\suncertify\db

And I have some test files in another directory:

C:\Rupert\myJavaFiles\test\db

In my Java files I include the statement package suncertify.db;and in my test files package test.db;

I include in my CLASSPATH the following C:\Rupert\myJavaFiles so that the whole CLASSPATH thing, when you type set classpath into a prompt looks like

CLASSPATH=.;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip;C:\Rupert\myJavaFiles

Then I can happily compile my files doing something like this:

C:\Rupert\myJavaFiles>javac suncertify\db\Contractor.java
C:\Rupert\myJavaFiles>javac test\db\ContractorTest01.java


But the real wrinkle happens with this:

C:\Rupert\myJavaFiles>java test\db\ContractorTest01
Exception in thread "main" java.lang.NoClassDefFoundError: test\db\ContractorTest01 (wrong name: test/db/ContractorTest01) ...


Even though it's a Windows machine you have to type the command:

C:\Rupert\myJavaFiles>java test/db/ContractorTest01

(slashes the other way) Have I got that right? Is that a reasonable way of doing it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

Even though it's a Windows machine you have to type the command:

C:\Rupert\myJavaFiles>java test/db/ContractorTest01

(slashes the other way) Have I got that right? Is that a reasonable way of doing it?


The java program takes the fully qualified class name as the parameter. So, it is...

C:\Rupert\myJavaFiles>java test.db.ContractorTest01


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Packages - is this a reasonable way to deal with them?