Author Topic: Packages  (Read 12471 times)

misplaced

  • 67% Incredible
  • Jackass I
  • Posts: 17
  • Karma: +10/-0
Packages
« on: May 04, 2005, 06:24:18 AM »
could someone give me a clue on how to create and import packages.....

the console output below the code explains my problem for me. (error message)

Code: [Select]

package mypack;  //Packed.java

public class Packed
{
int whocares;
public Packed()
{
whocares = 10;
}
public int get(){ return whocares; }
}


Code: [Select]

import mypack.*;  //Importer.java

public class Importer
{
Packed p;
public static void main(String[] args)
{
p = new Packed();
System.out.println(p.get());

}

}


Code: [Select]

C:\source>javac Packed.java
C:\source>javac -classpath C:\source\ Importer.java
Importer.java:1: package mypack does not exist
import mypack.*;
^
Importer.java:5: cannot access Packed
bad class file: C:\source\Packed.class
class file contains wrong class: mypack.Packed
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
        Packed p;
        ^
2 errors



I have also tried storing Packed.java and Packed.class in a new directory C:\source\mypack
If only I had a dime everytime I heard the words, "I sincerely don't think wedging a kitten in there is going to fix my hard drive"

Sang-drax

  • You created me, friend, you created me.
  • Jackass IV
  • Posts: 282
  • Karma: +10/-10
    • http://strandmark.com
Packages
« Reply #1 on: May 04, 2005, 07:51:30 AM »
Quote from: misplaced

I have also tried storing Packed.java and Packed.class in a new directory C:\source\mypack
You should do that. When Java searches for a class in a package it looks in %CLASSPATH%\packageName\className.class
 
But if you've tried that, I don't know.

Perspective

  • badfish
  • Jackass In Charge
  • Posts: 4635
  • Karma: +64/-22
    • http://jeff.bagu.org
Packages
« Reply #2 on: May 04, 2005, 11:37:25 AM »
what Sang-drax said is right. Your directory structure must exactly reflect your package structure.

package foo.bar.stuff.*;  must have the dir structure foo/bar/stuff/*.java

also, when you use -classpath or -cp you are overridding the entire classpath. So if your -classpath points to a single directory, then that is your entire classpath. Be sure to include every source directory and every .jar that you need on your classpath by using colon separated values.

If this doesnt help, post up the errors you get with the proper directory structure.

misplaced

  • 67% Incredible
  • Jackass I
  • Posts: 17
  • Karma: +10/-0
Packages
« Reply #3 on: May 04, 2005, 06:34:09 PM »
Appearantly now it recognizes the package...
Code: [Select]

C:\source>set CLASSPATH=C:\source;C:\source\mypack

C:\source>echo %CLASSPATH%
C:\source;C:\source\mypack

C:\source>javac C:\source\mypack\Packed.java

C:\source>javac Importer.java
Importer.java:7: cannot access Packed
bad class file: C:\source\mypack\Packed.class
class file contains wrong class: mypack.Packed
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
        Packed p;
        ^
1 error

C:\source>
If only I had a dime everytime I heard the words, "I sincerely don't think wedging a kitten in there is going to fix my hard drive"

misplaced

  • 67% Incredible
  • Jackass I
  • Posts: 17
  • Karma: +10/-0
Packages
« Reply #4 on: May 04, 2005, 06:44:51 PM »
...........ok, i tried qualifying Packed in the implementation and it worked...
mypack.Packed p = new mypack.Packed();   //compiles
Packed p = new Packed();            //does not compile and produces error in last my last post

i DO have the statement -
import mypack.*

in the Importer file
If only I had a dime everytime I heard the words, "I sincerely don't think wedging a kitten in there is going to fix my hard drive"

Perspective

  • badfish
  • Jackass In Charge
  • Posts: 4635
  • Karma: +64/-22
    • http://jeff.bagu.org
Packages
« Reply #5 on: May 05, 2005, 11:32:35 AM »
thats odd.

>>bad class file: C:\source\mypack\Packed.class

Did you try recompiling Packed.java?

misplaced

  • 67% Incredible
  • Jackass I
  • Posts: 17
  • Karma: +10/-0
Packages
« Reply #6 on: May 05, 2005, 11:59:47 AM »
yup..even deleted the Packed.class file to make sure it got recompiled
If only I had a dime everytime I heard the words, "I sincerely don't think wedging a kitten in there is going to fix my hard drive"

Perspective

  • badfish
  • Jackass In Charge
  • Posts: 4635
  • Karma: +64/-22
    • http://jeff.bagu.org
Packages
« Reply #7 on: May 05, 2005, 01:33:27 PM »
Everything there works fine for me, without even specifying the classpath.

You do have to make "p" static to access it in main though.

misplaced

  • 67% Incredible
  • Jackass I
  • Posts: 17
  • Karma: +10/-0
Packages
« Reply #8 on: May 06, 2005, 07:44:39 AM »
no....

i've tried just about everything too
If only I had a dime everytime I heard the words, "I sincerely don't think wedging a kitten in there is going to fix my hard drive"

hans

  • Guitar Addict
  • Jackass In Charge
  • Posts: 3523
  • Karma: +46/-18
Packages
« Reply #9 on: May 06, 2005, 10:50:00 AM »
Well, first off I'm going to suggest you use Ant  to do your compiling. It makes like very easy. But we'll assume you're trying to learn the jdk tools because you don't have Ant (or you're just into pain).

Let's do a little setup first (this is optional but the way I like to do it).
Create a src and bin folder (if you have jars then a lib folder is a good idea).

Create your package folder mypack in the src folder. Then create your java files src\mypack\Packed.java and src\Importer.java
OK, now we have our project structure organized and ready to compile.
from the comand line (assuming we're in the project root):

To compile the files (this way gets really ugly with many files/packages but works for this):
javac -d bin src\mypack\Packed.java src\Importer.java

Then to run our program:
java -classpath bin Importer.java

Here's a link with a strategy for compiling multiple files and packages:
http://www.yoda.arachsys.com/java/packages.html
This signature intentionally left blank.

hans

  • Guitar Addict
  • Jackass In Charge
  • Posts: 3523
  • Karma: +46/-18
Packages
« Reply #10 on: May 06, 2005, 10:52:21 AM »
Oh, and you'll want to fix your Importer class to declare the Packed object inside of your main method or you'll get a non-static variable access error.
Just a heads up.
This signature intentionally left blank.