Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing class in different file

 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John,

how do you compile it ?
 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

better try like this

 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu, Jesper,

Many thanks for all of your help.

I've been able to compile my classes using the following:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic