how to use a .class file with .java files
I'm quite green to java (I program in C/C++) and I still haven't figured out how this all works together.
I'm not trying to ask for anyone to do my homework, I'm just confused about some basic java stuff. I have an assignment which wants me to create a maze. The assignment comes with some files which have been pre-implemented and theoretically works. The folder gives me some .java files and some .class files. I opened the .java files and most of them are interfaces with no real code, so I'm assuming the meat of it are in these .class files. My question is, how do I interface with these .class files? do I need to do something like import myClass; if I have a file called myClass.class?
I've tried importing the file directory which these files are contained in (through Eclipse), but it seems the .class files don't show up. I'm guessing it's being processed in the background?
Here's 开发者_如何学Pythona link to the assignment http://www.ics.uci.edu/~goodrich/teach/ics23/LabManual/Dark/
Here's the zip file with all the .java and .class files http://www.ics.uci.edu/~goodrich/teach/ics23/LabManual/Dark/Dark.zip
Thanks in advance!
To add .class files to your project in Eclipse: right-click your project, select properties --> Java Build Path --> Libraries and click on Add Class Folder button. Select the folder with the classes.
No you won't need to import. Place the class files in the same folder so their path is included and simply call on them like normal datatypes.
For example:
myClass mc = new myClass();
or
(new myClass()).myRoutine();
If some method is static
myClass.staticalicious();
importing is used generally when path to classfiles needs to be specified.
Also, .class
files are simply compiled .java
files.
Oh and lastly, follow Java nomenclature :) It would be MyClass, not myClass.
.class
files are compiled versions of .java
files. Assuming that the .class
files are named and packaged appropriately you should be able to reference them like this:
Filename: some.package.class
Code: import some.package;
Once you've imported the package you can call methods on the classes contained therein.
As others mentioned, the class files are the compiled Java files, so no there is not "more meat" in them than you can see in the source files. There are class files for interfaces and class files for implementations.
I assume you just use the default package.
So just set the classpath to the directory holding these pre-compiled classes:
e.g. to compile your classes on a Linux/ Unix system:
javac -classpath <dirWithClassFiles>:. .....
and to run the application:
java -classpath <dirWithClassFiles>:. .....
You then can directly use those pre-compiled classes in your own classes.
class files are the binary output of the java compiler. they are the java equivalent of .o
files in C or C++ the "meat" in this instance is probably for you to implement.
The java compiler handles finding and including the necessary files, you may need to import some.package.name.*;
to avoid having to use full class names (e.g. SomeClass
instead of some.package.name.SomeClass
)
The class files are compiled classes and you cannot view them in your IDE. Your IDE will allow you to import them but just drop off the extension...
import example.some.Thing;
The .java files are the source files that were most likely used to compile the .class files. I'm sure they are that just for your reference.
Eclipse IDE does not show the .class files. you can add the jars or class files to your project build path. And then you can import the perticular class to your own class to use it.
精彩评论