[英]how to compile java with multiple files using command line
I am pretty new to Java and Linux. 我对Java和Linux很陌生。 I can't use an IDE but i have jdk installed (obviously).
我不能使用IDE,但我已经安装了jdk(显然)。 I have three .java files that i want to compile.
我有三个要编译的.java文件。 One is the main code file and two small classes.
一个是主代码文件,另外两个是小类。 how do i compile them using terminal?
我如何使用终端进行编译? these files are called:
这些文件称为:
- main.java
main.java
- object.java (Object.class when compiled)
object.java(编译时为Object.class)
- living.java (Living.class when compiled)
living.java(编译时为Living.class)
object.java and living.java only have a constructor for now that i want to call object.java和living.java现在只有一个构造函数,我想调用
i've tried 我试过了
javac main.java #this seems to be the right one
javac main.java object.java living.java
javac main.java Object.class Living.class
in terminal and 在终端和
import object.java;
import living.java;
import Object.class;
import Living.class;
import object;
import living;
import Object;
import Living;
in the main.java file 在main.java文件中
but nothing seems to work 但似乎没有任何作用
when i use 当我使用
import Living;
in the code it tells me that it misses a ; 在代码中告诉我它错过了一个; or .
要么 。
, when using precompiled ,使用预编译时
import Living.class
in the code i get 在代码中我得到
error: class, interface, or enum expected
import <Object.class>;
in the terminal and when i try 在终端上,当我尝试
import living.java
in the code i get 在代码中我得到
error: package living does not exist
import living.java;
in terminal 在终端
so what am i doing wrong? 那我在做什么错? do i have to import precompiled classes or java codefiles?
我必须导入预编译的类或Java代码文件吗? do i have to tell javac all files i want to use or only the main.java file?
我必须告诉javac我要使用的所有文件还是仅main.java文件? main.java compiles without error when i don't try to import one of the classes.
当我不尝试导入其中一个类时,main.java编译无错误。 And if i have to use .jar files please explain and give an example
如果我必须使用.jar文件,请解释并举一个例子
Your file name has to match the class name, eg if you have a class Living {...
your file name has to be named Living.java
. 您的文件名必须与类名匹配,例如,如果您有
class Living {...
文件名必须命名为Living.java
。 Be aware of the same character casing here. 请注意此处使用相同的字符大小写。 If you use
package xyz;
如果使用
package xyz;
in Living.java
, you also have to place your file in the subdirectory xyz
(eg xyz/Living.java
). 在
Living.java
,还必须将文件放置在xyz
子目录中(例如xyz/Living.java
)。
Importing is to be done by import Living;
进口将通过
import Living;
来完成import Living;
, with the same case. ,同样的情况。 On using
package xyz;
在使用
package xyz;
in your Living.java
, you have to use import xyz.Living;
在
Living.java
,必须使用import xyz.Living;
. 。 Classes within the same package doesn't need to be imported.
同一包中的类无需导入。
You compile your files by using javac Living.java
or with package javac xyz/Living.java
. 您可以使用
javac Living.java
或程序包javac xyz/Living.java
来编译文件。 The javac
will produce the Living.class
/ xyz/Living.class
file. javac
将产生Living.class
/ xyz/Living.class
文件。
Same with Main.java
. 与
Main.java
相同。
To run a classes main method, you have to run the java
executable with the class name, which contains the static void main(...)
method, eg java Main
(or java xyz.Main
if Main has a package xyz;
). 要运行一个类main方法,您必须运行带有类名称的
java
可执行文件,该类可执行文件包含static void main(...)
方法,例如java Main
(如果Main具有package xyz;
java xyz.Main
则为java xyz.Main
)。
Never create an Object.java
, since Object
is already reserved... 切勿创建
Object.java
,因为Object
已被保留...
BTW: maybe you follow one of the many tutorials available online, to get a first glance on java... 顺便说一句:也许您可以遵循在线上提供的许多教程之一,以快速了解Java ...
as @Arnaud commented: "Note that if all three classes are in the same package, you don't need to import them in your code" 正如@Arnaud所说:“请注意,如果所有三个类都在同一包中,则无需在代码中导入它们”
i don't need to import these classes in this case and leaving import away works. 在这种情况下,我不需要导入这些类,而无需进行导入工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.