importing for package

J

Javabean

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.
 
J

Javabean

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
;-)
 
J

Javabean

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.
 
O

Owen Jacobson

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
 
J

jussij

Javabean said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top