• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with packages and classpath

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have created a package called brianalbin and added a class named Heading to the package. The Heading class compiles fine.
I am importing the Heading class into another program and invoking a method of the class. Things work fine if I compile using the -classpath option, but the compiler can not find my Heading class without this option.
For example...
I save all source files in a directory called C:\Java\Source.
I save all class files in a directory called C:\Java\Classes.
Classes in the package brianalbin are saved in C:\Java\Classes\brianalbin.
Now...
From C:\Java I type
javac -d .\Classes -classpath ".\Classes" .\Source\Test.java
This works fine. However, without the classpath option it fails.
Regarding my system classpath variable it looks this on my XP machine.
path = %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\j2sdk_nb\j2sdk1.4.2\bin; C:\Java\Classes
Notice that C:\Java\Classes is there.
What am I doing wrong in setting this environment variable?
Brian
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to have modified the PATH environment variable, which Windows uses to find .EXE, .COM, and .DLL files, to include the directory where your classes are found; this is incorrect. Java uses an altogether different environment variable, CLASSPATH, to specify where classes should be found. It doesn't exist by default in Windows, you may well have to create it.
 
Brian Albin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest,
You are correct and I am now compiling just fine without the -classpath option.
One followup ...
Why can I not run the program from my root directory? For example from c:\Java I try ...
java .\Classes\Test to run the file Test.class
This produces an error so I have to cd into the Classes directory and type ...
java Test
and this works fine.
Any suggestions?
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can set the base directory through one of "java"'s command line arguments. So, from any working directory, you can specify the base classpath directory, along with any jar files to add to the classpath, followed by just the class name of your main, and you're set.
-d maybe?
 
Brian Albin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Figured it out ...
from C:\Java
java -cp .\Classes Test
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post in multiple forums. Some of us have answered this question in the beginners forum.
 
Brian Albin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry ...
Won't happen again.
reply
    Bookmark Topic Watch Topic
  • New Topic