• Post Reply Bookmark Topic Watch Topic
  • New Topic

Silly question on using multiple files  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've currently got 1 class in 1 file that I build via javac and run via java. Now I've got a class that's rather generic and don't want to include in my main class, but I want my main class to use it. In C I just make a .h file, compile both files, link and run. How do I do the equivalent in Java? I found packages but they seem too heavy for my little program.

In other words, I've got Foo.java and Bar.java. Foo wants to use Bar, both files live in the same directory, both are smallish (Foo about 350 lines, bar 100 including unit tests). What magic words should I feed google to figure out how to link/run them. (yes, I know link is the wrong word. But you know what I mean).

You'll notice I never mentioned the word 'Eclipse'. I've used Eclipse, use it for debugging, but I'm much happier in Vi.

 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No magic necessary. If the classes are in the same package, or if neither of them is in any package, then one can simply use the other one. To create a new Bar object you just write



... or whatever constructor parameters are necessary. To use static methods from the Bar class you just write



 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they are in the same package/folder you don't need to do anything.
You ARE actually using a package - the "default" package.
Classes in the same package are imported by default.
Just directly use one from the other.

So in your class Foo, create a variable of type Bar and go to town.

I would recommend you do actually declare packages for anything beyond the trivial (and probably for the trivial as well)

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
humph, can't be that easy.

type type typitty type

Holy Moose, it is that easy! Thanks!
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program with 2 .java files in the same directory works fine with javac. But when trying to run it in Eclipse I get errors about "Rle cannot be resolved to a type". The second file is Rle.java, which makes me think Eclipse wants some hand-waving before it uses it.

It's in the package view, the package explorer window looks like this:


How do I solve this?
 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like they are in different projects in your Eclipse setup, right? So, first question: Do you need to do that? Is there some reason why they can't be in the same project?

If this is just a simplified description of a real-life situation where the two classes have to be in separate projects, then you'd have to set Eclipse's build path in the first project to include the second project.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I just found Workspace/Rle/src/Rle.java, which is a week old and non-working version of the one I got working with javac.

How do I fix this. Learning Eclipse is harder than learning Java.

Got it, 30 minutes later. I ended up deleting Rle.java, adding a new file (Rle.java), closing the project, pulling Rle.java out of my VCS, re-opening the project, and all worked well.
 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There ya go. Yeah, it's true that Eclipse is an extra learning curve and it can get in the way of learning Java. A lot of people here would recommend you don't get into Eclipse etc. until you have a pretty good handle on Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!