AAAAAAAAAAAA
AAAAAAAAAAAA

Reputation: 37

How to import .class file into a .java file in eclipse correctly?

I need to import a few class files into a java project in eclipse. I put the files in a folder and then add the folder to my project by right clicking the project, then select Properties > Java build path, then "Add External Class Folder".

Then I imported the class into the java file using an import statement:

import cla11.classname;

("cla11" is the folder name, "classname.class" is the name of the class file.)

However, the compiler doesn't allow the import (import cla11 cannot be resolved) and the classes contained in the class files are hence unusable in the project (... (class name) cannot be resolved to a type).

Note: I am aware that my question is almost the same as the one in this link: https://stackoverflow.com/questions/2477947/how-to-import-class-file-in-a-java-file#= I used the method described in the answers, but the compiler does not allow it (as described). (Since I am not yet allowed to comment, the only way I could think of is to ask the same question again.(Any other suggestions on what I could do to solve such problems would be welcome.))

Upvotes: 2

Views: 3248

Answers (1)

bhdrkn
bhdrkn

Reputation: 6692

Class' package name and your folder name must match. For example, if your class' full name is com.example.MyClass you need a directory structure like com/example/MyClass.class and import the root folder to the Eclipse.

Update

I do not know your actual needs. Like why do you even need to import an external .class file. So you may prefer to create a Jar file and add it as dependency. IMHO, This way it will be a lot easier to distribute your application.

You can create a jar file like this jar cf my-library.jar com/ after that, you can add Jar file as a dependency. For example if you are developing a web application you can simply drop your my-library.jar file to ${project-root}/WebContent/lib directory. Or if you developing a console application, you can simply add your my-library.jar file to your class-path.

Upvotes: 3

Related Questions