aspose file tools*
The moose likes Beginning Java and the fly likes Accessing class in different file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing class in different file" Watch "Accessing class in different file" New topic
Author

Accessing class in different file

John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Hi folks,

I'm having a bit of trouble accessing classes in a seperate .java file and I cannot see what I'm doing wrong. They are in the same package too.

Any help would be greatly appreciated. Many thanks!

This is where I call the ActionListener class:


And the class is as follows:


The error is get is:
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


John,

how do you compile it ?
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Hi Balu,

I compile it by typing: javac gList.java

gList is the name if the class with the main method in.

Thanks
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Are the files in the same package structure ? or package ? ie is both in package guestList. ?

better try like this

John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
I've tried compiling like this:


But I'm still getting the same error:


I know there is something I'm doing wrong, but I've absolutely no idea what!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Your class btnSaveListener must be in a file named btnSaveListener.java in the directory guestList (because that's the name of the pacakge that the class is in). It will not work if you put this in a file named gList.java.

The name of the file must always be the same as the name of the public class that is in the file.

You should also put the base directory that contains the package directory in the classpath, so you should put "L:\My Projects\MyJavaApps" in the classpath, not "L:\My Projects\MyJavaApps\guestList".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Hi Jesper,

I added to the classpath and think it solved this problem as I am now getting 58 errors!

One of the errors it has picked up is this:


Initially, I had the miglayout folder in the 'MyJavaApps' folder, with the classpath pointing to it and it worked perfectly. Now I've added the above to the classpath, I get the above error. i've even tried moving the miglayout folder to the guestList directory, but still no luck.

I'm sorry if this is all really basic stuff.

EDIT: Forgot to mention that I didn't overwrite the classpath with the above, I added it on to the end.

Would I have to compile it like this?:
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


John,

One thing to note is javac -classpath will override the CLASSPATH variable. So you net.miginfocom.swing.*; jar should also be available in classpath.
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Balu, Jesper,

Many thanks for all of your help.

I've been able to compile my classes using the following:
 
 
subject: Accessing class in different file