为什么我们不使用带有“java”的.class扩展名。命令? [英] Why don't we use the .class extension with "java" command?

查看:135
本文介绍了为什么我们不使用带有“java”的.class扩展名。命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不在 java 命令之后给出 filename.class 文件,而不仅仅是 filename



假设我们要编译 test.java 程序,然后我们运行 javac test.java 。没关系!



之后会生成 test.class 文件,但要运行我们运行的程序 java test 而不是 java test.class 。这是什么原因?

解决方案

因为您没有描述要运行的文件。您告诉Java哪个包含main方法 - 类名称(在您的情况下) filename ,而不是 filename.class



字节码几乎总是包含在文件系统的文件中这一事实是一个实现细节。传递给 java 命令的类路径告诉它 where 查找类,然后主类参数告诉它使用哪个类。 / p>

javac 不同,因为此程序专门采用源文件并编译将它们转换为字节码。)


Why don't we give the filename.class file after java command, instead of only filename?

Suppose we want to compile the test.java program, then we run javac test.java. It is ok!

After that it will produce test.class file but to run the program we run java test instead of java test.class. What is the reason for this?

解决方案

Because you are not describing a file to run. You are telling Java which class contains the main method - and the class' name is (in your case) filename, not filename.class.

The fact that the bytecode is almost always contained in files on the filesystem is an implementation detail. The classpath you pass to the java command tells it where to look for classes, and then the main class argument tells it which class to use.

(It's different for javac, because this program specifically does take source files and compiles them into bytecode.)

这篇关于为什么我们不使用带有“java”的.class扩展名。命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆