简体   繁体   English

使用终端从Mac文件夹运行Java程序

[英]Running Java Program from Folder Mac with Terminal

So I'm used to creating a single Java file and putting it into the desktop and running it from the terminal on mac ( cd Desktop/ , javac HelloWorld.java , java HelloWorld ). 因此,我习惯于创建一个Java文件并将其放入桌面,然后从Mac上的终端( cd Desktop/javac HelloWorld.javajava HelloWorld )运行它。 Now I have a program that has multiple classes. 现在,我有一个具有多个类的程序。 My question is how to run a program from a folder. 我的问题是如何从文件夹运行程序。 I have around 5 .java files in my folder and I need to run the one with the main class. 我的文件夹中大约有5个.java文件,我需要使用主类运行该文件。

Writing multiple classes in single file or writing them in different .java files changes nothing. 在单个文件中写入多个类或在不同的.java文件中写入它们不会改变任何内容。 Finally, you have no. 最后,您没有。 of .class files equal to no. .class文件的数量等于no。 of classes you have defined in your .java files. 在.java文件中定义的类的集合。

The JVM checks for the public static void main(String[] args) in your class file to run the program(BTW you can also overload it but the above one is what is called first). JVM检查您的类文件中的public static void main(String[] args)以运行程序(顺便说一句,您也可以重载它,但上面的一个是第一个)。 You can also write main() method in every class. 您还可以在每个类中编写main()方法。 I believe that's why you use class name to run the program like java HelloWorld . 我相信这就是为什么您使用类名来运行java HelloWorld类的程序的原因。

You should just use the name of the entry point class instead of HelloWorld, other classes will be compiled and used automatically. 您应该只使用入口点类的名称而不是HelloWorld,其他类将被自动编译和使用。

The entry point class is the one that contains method 入口点类是包含方法的类

public static void main(String[] args)

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

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