0

there is a class called "Agent.class" that I wanna use inside of a java source file, and the .class file is in the same location as the .java file. the location is ./my/test/

when I tried to import this class by typing "import my.test.", Intellij-idea doesn't show autocomplete for the Agent.class but just the .java file.

inside the java file, a line of code Agent ag = new Agent(); failed because the compiler can't resolve the object "Agent".

What am I wrong here with the Intellij settings?

jad
  • 29
  • 3
  • Nothing wrong with IntelliJ. Your import is incorrect. Should be "import my.test.Agent;" This assumes that your Agent.java starts with "package my.test" – duffymo May 21 '19 at 14:06
  • Do you have a Agent.java file? Is Agent a class from a jar or it's your class? Did you copy .class file from another project and you don't have the source .java file? – Thanos M May 21 '19 at 14:37
  • I brought it over from a different project where this .class originally were in a different directory location. and I don't have its .java file. – jad May 21 '19 at 21:29
  • I also tried with "import my.test.Agent;" but the same result. – jad May 21 '19 at 21:30

1 Answers1

0

.class files are produced from compiled .java files, you need to have the Agent.java file in order to import Agent class to your own class. If you have the jar containing Agent class you can try this in Intellij:

  • ctrl+shift+alt+s to open Project Structure window
  • go to Libraries tab
  • press the add (+) button
  • select the path where your jar is and add it to your Module
Thanos M
  • 604
  • 6
  • 21
  • I don't have the source .java file and only importing it from another project. When open the .class it says package xxx.yyy.zzz; which is a different location. maybe this is the reason? – jad May 21 '19 at 21:39
  • Simply copy the Agent.java file to your project and you are ok. And yes this is probably the reason, you are importing a class that is originaly from a different package and you try to import it with my.test.Agent. Try to create a package with xxx.yyy.zzz name inside your project and move Agent.class there. If you cant find Agent.java file you can try use an online decompiler to produce .java file from .class file – Thanos M May 22 '19 at 07:15
  • to make use of the Agent.class, i need to either make it to a .jar file or .java source file? is there any way using it as its original .class format? – jad May 22 '19 at 21:50
  • Does this class has any other external dependencies ? If not just find the .java file online or decompile the .class by using an online decompiler. Have a look at this https://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files. I don't understand why you want to use this .class file exactly. – Thanos M May 23 '19 at 07:18
  • this is because i need to make a reference to an object of that class inside my code. – jad May 29 '19 at 22:28