How do I include .class files in my project in Eclipse? (Java)

前端 未结 6 1386
抹茶落季
抹茶落季 2020-11-29 09:12

Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the fil

相关标签:
6条回答
  • 2020-11-29 09:33

    Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder

    The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.

    0 讨论(0)
  • 2020-11-29 09:40

    You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.

    Right click into your project and select Java Build Path to add new dependencies.

    Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.

    0 讨论(0)
  • 2020-11-29 09:49

    You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.

    This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.

    Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.

    0 讨论(0)
  • 2020-11-29 09:55

    zip the class folder.

    Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file

    0 讨论(0)
  • 2020-11-29 09:57

    or put everything into a jar file and add this as an external jar.

    0 讨论(0)
  • 2020-11-29 09:58

    Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now two answers that truly answer the question in a long-term way.

    My original answer is left below for context.

    Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.

    A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.

    0 讨论(0)
提交回复
热议问题