2

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.))

10
  • 3
    if cla11 is in the build path, the import should not be needed Commented Aug 27, 2015 at 5:58
  • Have a look this stackoverflow.com/questions/661110/… Commented Aug 27, 2015 at 6:01
  • Put it into folder, eg. /external/cla11 and then add the /external folder as library. But you must be sure that the package of the class is declared as package cla11; Commented Aug 27, 2015 at 6:05
  • Show us where classname.class sits in your file system and show us the value of your CLASSPATH. Commented Aug 27, 2015 at 6:06
  • @SubodhJoshi Thank you for the suggestion. It seems to be the same problem as the question I mentioned in my question though. Commented Aug 27, 2015 at 6:34

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I need to import the external .class files for an exercise to test the .class files with JUnit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.