简体   繁体   English

从命令行编译

[英]Compiling from the command line

I'm trying to understand how works the option -classpath when compiling from command line. 我试图了解从命令行编译时如何使用-classpath选项。

I try from parent of mydirectory : 我尝试从mydirectory父母:

javac -cp mydirectory/subdir Hello.java javac -cp mydirectory / subdir Hello.java

But compiler says: 但编译说:

javac: no sources files javac:没有源文件

How does -cp ( -classpath ) work?? -cp-classpath )如何工作? What am I doing wrong? 我究竟做错了什么?

If I make it from subdir directory: 如果我从subdir目录中创建它:

javac Hello.java javac Hello.java

then compiles properly. 然后正确编译。

javac TestProgram1.java TestProgram2.java TestProgram3.java

You can use a wildcard to compile all the files in a folder, like this: 您可以使用通配符来编译文件夹中的所有文件,如下所示:

javac *.java

If you need to compile a lot of files at the same time but don't want to use a wildcard (perhaps you want to compile a large number of files but not all the files in a folder), you can create an argument file, which lists the files to compile. 如果你需要同时编译大量文件但不想使用通配符(可能你想编译大量文件但不想编译文件夹中的所有文件),你可以创建一个参数文件,列出要编译的文件。 In the argument file, you can type as many filenames as you want, using spaces or line breaks to separate them. 在参数文件中,您可以根据需要键入任意数量的文件名,使用空格或换行符将它们分开。 Here's an argument file named TestPrograms that lists three files to compile: 这是一个名为TestPrograms的参数文件,列出了三个要编译的文件:

TestProgram1.java
TestProgram2.java
TestProgram3.java

You can compile all the programs in this file by using an @ character, followed by the name of the argument file on the javac command line, like this: 您可以使用@字符编译此文件中的所有程序,然后使用javac命令行上的参数文件的名称,如下所示:

javac @TestPrograms

-cp and -classpath -cp和-classpath

Specifies where to find user class files. 指定查找用户类文件的位置。 Use this option if your program makes use of class files that you've stored in a separate folder. 如果您的程序使用您存储在单独文件夹中的类文件,请使用此选项。

The files you are compiling must be defined directly 您要编译的文件必须直接定义

For example, if you are in the parent folder: 例如,如果您在父文件夹中:

javac subdir/Hello.java

would be needed to compile. 需要编译。

The classpath allows finding .class files that are needed to compile things defined as above. 类路径允许查找编译上面定义的内容所需的.class文件。

For example, if in the code you had a reference to the class "Birds" and that class was in a jar named "animals.jar" in the current folder, which was the parent of where the java file was, you would need this: 例如,如果在代码中你有一个类“Birds”的引用,那个类在当前文件夹中名为“animals.jar”的jar中,它是java文件所在的父类,你需要这个:

javac -cp animals.jar subdir/Hello.java
javac XXX.java

Command used to compile java class , where as -classpath flag is available with java command. 用于编译java类的命令,其中-classpath标志可用于java命令。

we run java program by using 我们用java运行java程序

java XXX.class

here to start with main() function, JVM should know where the class file is. 这里以main()函数开头,JVM应该知道类文件的位置。 so to specify path of class we use classpath flag 所以要指定类的路径我们使用classpath标志

Classpath link to get more details. Classpath链接以获取更多详细信息。

some example for runing java program are 运行java程序的一些例子是

java -classpath .;yourJarFile.jar your.MainClass

 java -classpath path_till_classfile/HelloWorld

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM