• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Package and Classpath problem

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a grand ol' time trying to figure out what is probably a no-brainer for most of you. I am trying to learn the fine art of package development and just can't get this to work. I am using sdk 1.4 and win 2000. My classpath (I've tried it in the user and/or system sections) is .;c:\com\jwilson\pkgs (I have also tried including c:\janets-java and that didn't make a difference)
There are 2 programs. The one with the package is stored in the c:\com\jwilson\pkgs subdirectory with the name: Employee.java This one compiles fine and look like:


The fun begins when I try to compile the next program which is stored in the c:\janets-java directory:

The error I receive is:
C:\janets-java\PackageTest.java:1: package com.jwilson.pkgs does not exist
import com.jwilson.pkgs.*;
^
C:\janets-java\PackageTest.java:7: cannot resolve symbol
symbol : class Employee
location: class PackageTest
Employee janet = new Employee("Janet Wilson", 10000);
^
C:\janets-java\PackageTest.java:7: cannot resolve symbol
symbol : class Employee
location: class PackageTest
Employee janet = new Employee("Janet Wilson", 10000);
^
3 errors
I'm sure this is probably staring me in the face, but I'm just not seeing it. Can someone please help me out?
BIG thanks! Janet
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you compile Employee.java it will create an Employee.class file. Since your classpath is pointed to c:\com\jwilson\pkgs and the package for Employee.java is com.jwilson.pkgs then your Employee.class file should actually reside in c:\com\jwilson\pkgs\com\jwilson\pkgs
Let me describe how I set up my directory structure and maybe this will clear things up. I have a Dev_Projects directory with a myjava under it. C:\Dev_Projects\myjava
Under that I have two directories called src and lib. If I have a file called foo.java which is in the com.stuff.gui package then my source code will be in the C:\Dev_Projects\myjava\src\com\stuff\gui directory. When I compile the program I can do one of 2 things: I can either use "ant" (which I always do) and tell it where to dump my class files. OR, I can use the -d command option on my javac command to tell it to place all my class files in C:\Dev_Projects\myjava\lib
Now, once that's compiled my foo.class file will be in the C:\Dev_Projects\myjava\lib\com\stuff\gui directory. Therefore, my classpath must point to C:\Dev_Projects\myjava\lib
Once my classpath gets the javac or java program to look at C:\Dev_Projects\myjava\lib the program will then automatically append the package name onto the directory. Therefore, since your package is com.jwilson.pkgs your java programs will go to the directory specified in your classpath and THEN look for a com/jwilson/pkgs directory underneath that directory.
So, assuming you to have a C:\Dev_Projects\myjava directory with src and lib directories then you would have to do this:
1. set your classpath to .;C:\Dev_Projects\myjava\lib
2. Put your Employee.java and PackageTest.java files in C:\Dev_Projects\myjava\src\com\jwilson\pkgs
3. Compile with the javac -d C:\Dev_Projects\myjava\lib option
4. Check to see if you now have a C:\Dev_Projects\myjava\lib\com\jwilson\pkgs\Employee.class file (and the PackageTest.class file as well)
Now if you type
java com.jwilson.pkgs.PackageTest
you should have a working program.
I hope that helps.
Greg
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,
BIG BIG THANKS! I would have never figured this out in a million years. Everything seems to be running smoothly and now I can try out different options to figure out what would be more conducive to our environment (e.g.-having developers put their specialized code into one area and having the packages in a more generalized area on the network).
I think for now, I'll just resort to the -d compile option vs. trying to read through the stuff about Ant. I've got enough on my hands just learning Java!
Again, thanks a million
Janet
 
Yeah, but is it art? What do you think tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic