0

I have homework that asks me to use a .class file for my project, but I don't know how to use those files in a .java file. This is the project structure

agentes.class
datos_confidenciales.class
II_Parcial_2007_1.doc
Main.java
numeros.class

I mean, I got to make a class called Main that implements the numeros interface, the numeros interface is in numeros.class file

I've googled but no results.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
dylanroman03
  • 13
  • 1
  • 5
  • 2
    The basic way is to put the `.class` files on your class path so the compiler can find them. In an IDE this may involve adding the files as a "library" and including the library as part of the project. You should have instructions / documentation how to do that for your assignment. If you don't, I'd ask a classmate or your instructor, I think you may have missed some instructions. – markspace Nov 18 '22 at 15:23
  • P.S. a `.doc` file is normally a Microsoft Word document, maybe open it up and see what's inside? – markspace Nov 18 '22 at 15:26
  • 1
    Even if it’s not a Word file, a file with a `.doc` ending typically is a readable text document targeting human users. So it’s definitely worth looking into this file first. – Holger Nov 18 '22 at 15:34
  • @markspace yeah is a word document, with the instructions for the homework, but it doesn't say anything about how to use the .class file – dylanroman03 Nov 18 '22 at 21:13
  • 1
    Without any details what you were actually told to do, I don't think anyone here has a chance of actually helping you. Please ask your instructor what you are supposed to do. – markspace Nov 18 '22 at 21:32
  • If your teacher is reasonable, he will be delighted to give you all the necessary requirements (i.e. contract) on how the interface must be implemented to fully comply with the specification at hand. Otherwise an interface is just technical debt to urgently get rid of. – MJG Nov 20 '22 at 17:18

2 Answers2

-1

The .class files simply need to be on the classpath when you compile. How exactly you configure that depends on how you compile (on the commandline or in an IDE; in what IDE).

Here is the documentation for the classpath. Here is a related question for setting the classpath.

The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • But the code would mark an error, and the autocomplete would not work – dylanroman03 Nov 18 '22 at 21:24
  • @dylanroman03 I don't think so. It is a very frequent thing to use libraries (3rd party jar files) which just contain the class files. Static analysis and code completion should work without problems. – kutschkem Dec 09 '22 at 09:00
-2

One option is to decompile the class-file, which will get you a java-file of the interface that was compiled.

If you don't want to use a decompiler, write a small application that will reflect the numeros class and prints out all methods and their parameter-types and return-types, so you can implement it.

Of course you could always use an IDE, with the class-file in module-path, your IDE will assist you to implement the interface.

MJG
  • 355
  • 1
  • 9
  • 3
    There is no need to use a decompiler here, this is nonsense.. – Gyro Gearless Nov 18 '22 at 15:47
  • @GyroGearless For myself, yes, that is also true. For somebody that's just beginning Java, maybe it's something to try.. only if no IDE is at hand here. *In reality*, to implement 'correctly' an 'interface', you must know the contract, which is not possible w/o source-code or at least any kind of specifications on the requirements on how to implement a given interface . – MJG Nov 20 '22 at 16:51
  • 1
    @MJG since a decompiler is not capable of restoring documentation, it doesn’t help in explaining the contract of an interface. When you want to know, which methods a compiled interface has, you can use `javap` which ships with every JDK (and is not a decompiler). – Holger Dec 12 '22 at 16:17
  • @Holger a decompiler is used when the IDE does not find the sources, it will at least give you the 'structure' of the class. The only option I know from using a compiled interface is to reverse engineer and then of course I agree, to understand an interface, of course it is not what you need, as I indicated above. It both helps to implement the interface in a compilable way, not more. – MJG Dec 13 '22 at 13:12