简体   繁体   English

在 Java 上运行 Main 之外的程序

[英]Running Programs outside of Main on Java

I'm a new CS student just starting out with Java, and currently I'm using IntelliJ as my IDE.我是一名刚开始使用 Java 的 CS 新学生,目前我使用 IntelliJ 作为我的 IDE。 At the moment, we aren't going over any crazy complicated programs, just basic in class projects.目前,我们不讨论任何疯狂的复杂程序,只是课堂项目的基本内容。 My problem is that when I go to create my project, I have no choice but to run it through the Main class, even though the instructions want the class name to be something specific.我的问题是,当我去创建我的项目时,我别无选择,只能通过 Main 类运行它,即使说明希望类名是特定的。 Also, whenever I try making a new class or running a java file I've downloaded, it still continues to run the Main class, even if I have Main closed.此外,每当我尝试创建一个新类或运行我下载的 java 文件时,它仍然会继续运行 Main 类,即使我关闭了 Main。 I have files that I created inside of VS Code (I'm aware that's not the best option, and that's why I switched to IntelliJ), and I'm unable to run them unless I create a brand new project, copy and paste the code into the Main file, and change the class name to Main.我有我在 VS Code 中创建的文件(我知道这不是最好的选择,这就是我切换到 IntelliJ 的原因),除非我创建一个全新的项目,否则我无法运行它们,复制并粘贴将代码写入 Main 文件,并将类名更改为 Main。

I'm not sure if this is something inside of Java, IntelliJ, or just me not understanding how everything works yet, but I was hoping someone can help me out with this.我不确定这是否是 Java、IntelliJ 内部的东西,或者只是我不明白一切是如何工作的,但我希望有人能帮我解决这个问题。

Here's an example.这是一个例子。 In the src folder, I have the package folder and inside of this is the Main java file along with a separate file that I'm trying to run.在 src 文件夹中,我有包文件夹,其中包含 Main java 文件以及我尝试运行的单独文件。 Main is also empty.主要也是空的。

package com.ethan;

import java.util.Scanner;


public class Exercise02_19 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        //Enter 3 points
        System.out.println("Enter the 3 points for a triangle: ");

        //Point A
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();

        //Point B
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        //Point C
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();


        //Find the sides and area

        double side1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        double side2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
        double side3 = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));

        double s = (side1 + side2 + side3)/2;
        double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

        System.out.println("The area of the triangle is: " + area);

    }
}

Thank you.谢谢你。

Having your files open on an editor or on an IDEA does not mean that they are closed or open for running.在编辑器或 IDEA 上打开您的文件并不意味着它们已关闭或打开以供运行。 They are just open to modification.他们只是开放修改。

In order to run a Java program, you need a main method (unless you are using a framework which in that case it is hidden from you).为了运行 Java 程序,您需要一个 main 方法(除非您使用的是在这种情况下对您隐藏的框架)。 Everything runs through main, so if you want to use a custom class you have to call it through main.一切都通过 main 运行,所以如果你想使用自定义类,你必须通过 main 调用它。

The main method looks like this main方法看起来像这样

public static void main(String[] args){
     // your code
}

You can run the program either by using the IDEA, by setting the JDK or by compiling it with javac and then running the .class file with java command.您可以通过使用 IDEA、设置 JDK 或使用javac编译它然后使用java命令运行.class文件来运行该程序。 There are plenty of articles on how to run your program through CLI.有很多关于如何通过 CLI 运行程序的文章。 I would recommend using the CLI at first in order to understand what's going on JVM behind the scenes.我建议首先使用 CLI 以了解幕后 JVM 上发生的事情。

It does not depend on the classes name but on the main method which is the entry point for code execution.它不依赖于类名,而是依赖于作为代码执行入口点的 main 方法。

This method must be defined inside a class and looks exactly like this:这个方法必须在一个类中定义,看起来完全像这样:

public static void main(String[] args){
// code goes here
}

Make sure this method exists only once in your application.确保此方法在您的应用程序中只存在一次。

To create this method fast in IntelliJ type psvm and then tab.要在 IntelliJ 中快速创建此方法,请键入psvm ,然后使用 Tab。

The name of the class you use to invoke the JVM determines which main method is called when you define more than one main method.用于调用 JVM 的类的名称决定了在定义多个 main 方法时调用哪个 main 方法。

@efan - Alexis Pavlidis is correct. @efan - Alexis Pavlidis 是正确的。

To elaborate:详细说明:

  1. In Java, every "program" consists of one or more " classes ".在 Java 中,每个“程序”都由一个或多个“”组成。 In your example, the class happens to be called Exercise02_19 .在您的示例中,该类恰好被称为Exercise02_19

  2. Every program must "start somewhere".每个程序都必须“从某处开始”。 This is called an entry point .这称为入口点

  3. In Java, the entry point is a method with the signature public static void main(String[] args) .在 Java 中,入口点是一个带有签名的方法public static void main(String[] args)

  4. In Java, unlike many other languages, each class may have its own main() .在 Java 中,许多其他语言不同,每个类都可能有自己的main() You must choose which entry point at runtime.您必须在运行时选择哪个入口点。 With your IDE, or with command line arguments.使用您的 IDE 或命令行参数。

    In your case, you choose the main() method in class Exercise02_19 .在您的情况下,您选择了类Exercise02_19main()方法。

  5. As Alexis Pavlidis said:正如亚历克西斯·帕夫利迪斯所说:

    There is no Main class, the only requirement for a java application is a main method.没有 Main 类,Java 应用程序的唯一要求是 main 方法。 So your class can be named whatever you want.所以你的类可以随意命名。

A java program will always start in the main() method. Java 程序总是main()方法开始。 You can imagine it as your entry point.你可以把它想象成你的切入点。

If you want to create separate classes, you must call them from your main method.如果要创建单独的类,则必须从 main 方法中调用它们。 Here is an example:下面是一个例子:

Main.java主程序

public class Main {

    public static void main(String[] args) {
        MyTestClass myInstanceOfIt = new MyTestClass();
        myInstanceOfIt.doSomething();
    }
}

MyTestClass.java我的测试类.java

public class MyTestClass {

    public void doSomething() {
        //do something here
    }
}

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

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