0

I am wondering how to use an existing .class file in my Java Intellij project. How do you include an existing class in a project? I don't have the .java file, just the .class file. IntelliJ lets me paste in the package containing the .class file, but acts as if there is nothing in the package.

I've used existing classes in my projects before by compiling from the command line, but I would like to know how to do it in Intellij.

jabe
  • 784
  • 2
  • 15
  • 33
  • Possible duplicate of [Referencing a .class file in IntelliJ Java Project](http://stackoverflow.com/questions/12133823/referencing-a-class-file-in-intellij-java-project). See if the solutions suggested there works. If not, post details of what did not work here. – hitz May 15 '15 at 21:31
  • That question seems similar and when I import my classes as a library they do show up in the Project view. However, I can still not reference them in my code. I would like to import the class, but it doesn't let me. Do you have any other ideas? It seems like I'm almost there, as intellisense can see the class I want to use, but it won't let me import it. – jabe May 15 '15 at 22:03

1 Answers1

2

You can include this .class file to the classpath or add it to the dependencies of the project.

Alex
  • 171
  • 1
  • 8
  • What do you mean by including it to the classpath, how is that done? I have tried adding it as both a directory and a module dependency and both do show the class, but I still cannot access the class in my code. (I don't know the difference between the two). – jabe May 15 '15 at 22:16
  • What is full class name? If it is foo.xyz.Clazz and Clazz.class file is in directory /users/me/allclasses/foo/xyz (so full name of the file is /users/me/allclasses/foo/xyz/Clazz.class) then classpath should have mention of the /users/me/allclasses as one of the points. That's how Java works. It looks complicated but it doesn't matter because usually nobody works with individual files. Usually they are packed in jars and/or included to the projects. – Alex May 16 '15 at 02:21
  • It is so weird, when I add my class's folder as either a Project Library or a directory, the class seems to add fine. The errors in the editor go away and it knows the class I'm talking about. In my case the class is in a folder called "Secure". However, when I try to make the file, it gives me errors saying 'package Secure does not exist', which is so frustrating because it is clearly there. – jabe Jul 02 '15 at 22:37
  • In Java class world package means folder. So, if you have class abc.xyz.Test then in one of you points in the classpath should be folder abc with folder xyz where Test.class resides. For example for class path c:\tmp;c:\allLibraries;c:\MyHome\MyLibs one of them should have it. For example c:\MyHome\MyLibs\abc\xyz\Test.class – Alex Jul 07 '15 at 01:32
  • So my class is called "Bar.class" and is in a folder called "Foo". I went to my module settings and added the "Foo" folder as a dependency by clicking the "plus" button and then "add JARs or directories". I can see the package/folder now under my list of External Libraries with Bar.class in it. But when i go to build or compile the project, it claims "package Foo does not exist". I must be overlooking something. Thanks for all your help btw. – jabe Jul 07 '15 at 17:27
  • I got it to work. My folder was "Foo" and contained the class "Bar.class". I kept trying to add "Foo" as a dependency which wasn't working. I put "Foo" inside a different folder, "Classes" and added "Classes" as a dependency instead of "Foo". It worked! Again, thanks for your help. – jabe Jul 07 '15 at 17:34