简体   繁体   English

如何在Java中为visual studio代码修复“错误:在应用程序类路径上找到的类:Main”

[英]How to fix "error: class found on application class path: Main" in java for visual studio code

this is my first time posting here and would like how to solve this error message.这是我第一次在这里发帖,想知道如何解决此错误消息。 It appears only sometimes and only lets me build on a program called Main.java.它只是有时出现,并且只让我建立在一个名为 Main.java 的程序上。 I'm a begginer programmer so please bear with me, this is the code im trying to run:我是一个初学者程序员,所以请耐心等待,这是我试图运行的代码:

import java.util.Scanner;
import java.text.NumberFormat;


public class Main {
    public static void main(String[] args) {

        Scanner priceScanner = new Scanner(System.in);
        System.out.print("Price: ");
        int price = priceScanner.nextInt();

        Scanner interestScanner = new Scanner(System.in);
        System.out.print("Interest rate: ");
        double interest = interestScanner.nextDouble();

        Scanner numberOfPaymentsScanner = new Scanner(System.in);
        System.out.print("Number of payments: ");
        int numberOfPayments = numberOfPaymentsScanner.nextInt();

        Double monthlyInterest = interest / 1200;

        Double result = ((double)price * ((interest * Math.pow((1 + 
interest), (double)numberOfPayments))/((Math.pow((1 + interest), 
(double)numberOfPayments)) - 1)));

        NumberFormat currency = NumberFormat.getCurrencyInstance();
        String mortgage = currency.format(result);

        System.out.println("Your mortgage is: " + mortgage);
    }
}

I haven't seen any comprehensible ways to solve this problem online, and the only thing i think could solve it is to reinstall java in another drive and change the classpath.我还没有在网上看到任何可以理解的方法来解决这个问题,我认为唯一可以解决的就是在另一个驱动器中重新安装 java 并更改类路径。

Thanks for your attention.感谢您的关注。

I solved it - my mistake.我解决了 - 我的错误。 While executing the program using the terminal I was typing java Main.java , whereas the correct execution method was to type java Main .在使用终端执行程序时,我输入的是java Main.java ,而正确的执行方法是输入java Main

Step 1:第1步:

javac + Filename.java javac + 文件名.java

Step 2:第2步:

java + Filename // execute without add .java java + Filename // 执行时不添加 .java

With Single-file source-code programs which is a new way of executing 1 File Java programs is only available since Java 11 .使用单文件源代码程序,这是一种执行 1 文件 Java 程序的新方法,仅在Java 11 之后可用。 You can run the command: java (Java File Name without .java extension)您可以运行以下命令:java(没有 .java 扩展名的 Java 文件名)

java Main.java

Though please take into consideration that this way of executing only works if your Java project is only 1 Java File.但请注意,这种执行方式仅在您的 Java 项目只有 1 个 Java 文件时才有效。

FYI: This single-file source code will be executed fully in memory and you can only import code that came with the JDK you are working.仅供参考:这个单文件源代码将在内存中完全执行,您只能导入您正在使用的 JDK 附带的代码。 Finally if you want your code to run as fast as possible compile with javac before executing you program.最后,如果您希望代码尽可能快地运行,请在执行程序之前使用 javac 进行编译。

javac Main.java

java Main

Just be careful that there is no Main.class already in the folder, this may cause a confusion to the compiler.请注意文件夹中没有 Main.class,这可能会导致编译器混淆。

您需要将其作为 java App 而不是 java App.java 运行

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

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