如何运行java类命令行?(How to run java class command line?)

编程入门 行业动态 更新时间:2024-10-22 09:51:03
如何运行java类命令行?(How to run java class command line?)

我有以下测试问题:

鉴于:

1. package com.company.application; 2. 3. public class MainClass { 4. public static void main(String[] args) {} 5. }

MainClass存在于/ apps / com / company / application目录中。 假设CLASSPATH环境变量设置为“。” (当前目录)。在命令行输入的两个java命令是否会运行MainClass ? (选择两个。)

A. java MainClass如果从/ apps目录运行 B. java com.company.application.MainClass如果从/ apps目录运行 C. java -classpath / apps com.company.application.MainClass如果从任何目录运行 D. java -classpath。 MainClass如果从/ apps / com / company / application目录运行 E. java -classpath / apps / com / company / application:。 MainClass如果从/ apps目录运行 F. java com.company.application.MainClass如果从/ apps / com / company / application目录运行

我认为正确的是D和E,但是B和C,我想知道为什么?

I have the following test question:

Given:

1. package com.company.application; 2. 3. public class MainClass { 4. public static void main(String[] args) {} 5. }

And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH environment variable is set to "." (current directory).Which two java commands entered at the command line will run MainClass? (Choose two.)

A. java MainClass if run from the /apps directory B. java com.company.application.MainClass if run from the /apps directory C. java -classpath /apps com.company.application.MainClass if run from any directory D. java -classpath . MainClass if run from the /apps/com/company/application directory E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory F. java com.company.application.MainClass if run from the /apps/com/company/application directory

I think the correct are D and E but the are B and C and I am wondering why?

最满意答案

您应该使用其全名来执行类。 类的全名包括:

<package location> + <simple name of class>

考虑到这一点, MainClass全名是com.company.application.MainClass 。

Java可以从包含树包中的顶级目录的目录中找到此类。 从示例中,根目录是/apps :

apps <-- root + com <-- here the package starts + company + application - MainClass.class

所以这个根目录应该在类路径中,以便Java程序(JVM)可以访问已编译的类和应用程序的入口点。


从问题:

假设CLASSPATH环境变量设置为“。” (当前目录)。

选项B指出:

B. java com.company.application.MainClass 如果从/ apps目录运行

由于您位于/apps目录中且当前目录是CLASSPATH,因此Java可以找到要执行的指定类。

选项C指出:

C. java -classpath /apps com.company.application.MainClass 如果从任何目录运行

在这种情况下,您将/apps设置为CLASSPATH,因此Java可以找到要执行的指定类。

所有其他选项都无法完成上述说明。

You should execute a class using its full name. The full name of a class consists of:

<package location> + <simple name of class>

With this in mind, MainClass full name is com.company.application.MainClass.

Java can locate this class from the directory that contains the top directory in the package tree. From the example, the root directory is /apps:

apps <-- root + com <-- here the package starts + company + application - MainClass.class

So this root directory should be in the classpath in order that Java program (the JVM) can access to the compiled classes and the entry point of the app.


From the question:

Assume the CLASSPATH environment variable is set to "." (current directory).

Option B states:

B. java com.company.application.MainClass if run from the /apps directory

Since you're in /apps directory and the current directory is the CLASSPATH, so Java can locate the designated class to execute.

Option C states:

C. java -classpath /apps com.company.application.MainClass if run from any directory

In this case, you're setting /apps as CLASSPATH, so Java can locate the designated class to execute.

All the other options fails to accomplish the explanation above.

更多推荐

本文发布于:2023-08-07 18:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   run   java   line   command

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!