-1

I know this has been asked a few times, but I can't find a solution using those answers.

I have a class file called LRW.class which I have put in an folder called external in the eclipse folder under my program name. So my path to the class is: myusername.WorkBench.ProjectA.external.LRW.class and I've made that folder using the new class folder in eclipse.

How do I use/import/call this class in java?

TIA

James MV
  • 8,569
  • 17
  • 65
  • 96

3 Answers3

1

If it's in the default package, being used by a class in the default package, you don't need to import it at all. If the class using LRW isn't in the default package, it would be easiest to put LRW in a package--you can't import classes in the default package (see JLS section 7.5).

If it complains it isn't found, you'll need to make sure that folder is listed in the project's build path (right-click project -> properties, Java build path, Libraries tab, Add Class Folder).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • It doesn't seem to work still, I've tried setting the folder as a class folder and external class folder, they appear in the structure, but as soon as I type in import LRW; eclipse shows an error with no useful fix? – James MV Oct 29 '11 at 14:56
  • @EricBanderhide Is the class really there? Is the `LRW` class *really* in the default package, or did it have a `package` statement in it? – Dave Newton Oct 29 '11 at 14:59
  • I don't fully understand, its just a class file I placed in a folder next to the src folder in its eclipse folder? – James MV Oct 29 '11 at 15:00
  • @EricBanderhide - I think you need to do a Java tutorial to learn what a package is in Java. You shouldn't use SO as a way to avoid reading the textbook / tutorial. – Stephen C Oct 29 '11 at 15:05
  • 1
    @EricBanderhide I understand that, but a class has a package, either the default, or a named package. Your import must match the package of the class, and the class file must be located in a directory matching its package name. One way to determine what the package is to run `javap LRW` from the command line to see if it needs to go into a sub-directory under `external`. – Dave Newton Oct 29 '11 at 15:09
  • @EricBanderhide That's just where it is in the folder; the *class file itself* was compiled either the default or a named package. Since your class is *also* in the default package (boooo) you don't even need to import it if it *was* compiled in the default package. – Dave Newton Oct 29 '11 at 15:23
  • @EricBanderhide I pretty much already told you. *I* don't know anything about the LRW class other than what you've told me, and can't run `javap` on it to check. Try without the import. See if it's supposed to be in a package. Make sure the folder is listed correctly in the build dependencies (as a class folder). If *all* else fails, create a public git repo containing only the source and this single class file, in their current folder hierarchy, and provide a link to the project and I'll take a look. – Dave Newton Oct 29 '11 at 15:39
0

it is correct, you would use "import LRW" however it is a bad practice not to use namespaces. You should better put the class in a folder called , for example "com/mydomainn" and then use

import com.mydomain.LRW

user986280
  • 131
  • 3
  • 3
0

Add external to your build path as class folder and use LRW like any other class.

Make sure LRW is in default package, otherwise you need to change it's location according to the package.

Community
  • 1
  • 1
Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65