importing for package

Discussion in 'Java' started by Javabean, Mar 1, 2006.

  1. Javabean

    Javabean Guest

    Hi all,

    I'm trying to understand the concept of packaging which seems relatively
    simple and clean, following the directory concept. To test this I've written
    two files: 1 which defines a class and puts it inside a package and 1 which
    tries to import the class defined in the package.

    This is the code for MyClass.java:

    package mypackage;

    public class MyClass {
    public void greeting(){
    System.out.println("Hello!");
    }
    }

    I have saved this file to c:\java\mylib\mypackage. The CLASSPATH variable is
    set as follows : CLASSPATH = .;c:\java\mylib


    And the code for Exercise2.java (which tries to import the MyClass file):

    import mypackage.*;

    public class Exercise2 {
    public static void main(String[] args){
    MyClass m = new MyClass();
    m.showMe();
    }
    }


    The MyClass class seems to get imported, however I get an error message when
    trying to call the showMe() method.

    C:\java\test\Exercise2.java:6: cannot find symbol
    symbol : method greeting()
    location: class mypackage.MyClass
    m.greeting();
    ^
    1 error

    Anyone have any idea why this is? The greeting() method is declared as
    public inside MyClass so I should have access to it... I also tried defining
    a no-arg constructor for MyClass which produces a text message but the text
    message was never printed as well (even though the object seems to be
    created without errors?!)

    Thanks for any input.
    Javabean, Mar 1, 2006
    #1
    1. Advertising

  2. Javabean

    Javabean Guest

    > And the code for Exercise2.java (which tries to import the MyClass file):
    >
    > import mypackage.*;
    >
    > public class Exercise2 {
    > public static void main(String[] args){
    > MyClass m = new MyClass();
    > m.showMe();
    > }
    > }


    m.showMe() should read: m.greeting(). I made a mistake with copy/pasting the
    code. In my code the method names are identical. So this is not the problem
    ;-)
    Javabean, Mar 1, 2006
    #2
    1. Advertising

  3. Javabean

    Javabean Guest

    Figured it out myself. For some reason the CLASSPATH variable wouldn't get
    propagated to UltraEdit, from which I compile and run the programs. I had to
    explicitly give the classpath as an argument to the javac and java commands
    to make it work (e.g. java -classpath ".;c:\java\mylib" Exercise2). Still it
    boggles me why UltraEdit doesn't seem to use the classpath environment
    variable while it works perfectly from the command line prompt.
    Javabean, Mar 1, 2006
    #3
  4. On Wed, 01 Mar 2006 13:18:51 +0100, Javabean wrote:

    > Figured it out myself. For some reason the CLASSPATH variable wouldn't get
    > propagated to UltraEdit, from which I compile and run the programs. I had to
    > explicitly give the classpath as an argument to the javac and java commands
    > to make it work (e.g. java -classpath ".;c:\java\mylib" Exercise2). Still it
    > boggles me why UltraEdit doesn't seem to use the classpath environment
    > variable while it works perfectly from the command line prompt.


    Under Windows, the 'SET' command doesn't propagate back to the GUI. Check
    under Control Panel/System/Advanced/Environment Variables instead, then
    *restart UltraEdit*. Windows programs acquire their environment at
    startup, and don't receive changes from the outside world.

    -Owen
    Owen Jacobson, Mar 1, 2006
    #4
  5. Javabean

    Guest

    Javabean wrote:

    > Still it boggles me why UltraEdit doesn't seem to use the classpath
    > environment variable while it works perfectly from the command line
    > prompt.


    As a test you could create a batch file that looks like this:

    Set classpath

    and run this batch file from within UltraEdit. The captured
    output should show the current system wide classpath value.

    If the classpath displayed above is not complete then you
    need to use the control panel to setup the classpath.

    Jussi Jumppanen
    Author: Zeus for Windows Java IDE
    http://www.zeusedit.com
    , Mar 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mhk
    Replies:
    1
    Views:
    600
    Chris Smith
    Nov 28, 2003
  2. Alex
    Replies:
    0
    Views:
    496
  3. maverick
    Replies:
    0
    Views:
    290
    maverick
    Jul 17, 2006
  4. Dave
    Replies:
    2
    Views:
    446
  5. plb
    Replies:
    2
    Views:
    317
Loading...

Share This Page