我的Java程序未编译-递归字符串

问题描述 投票:0回答:1

嗨,我是一名 IT 学生,目前正在做一些作业,但对于我的一生,我不明白为什么它没有编译我的 IDE (IntelliJ) 说我的程序没有任何问题,但每次我尝试运行它时,我都会得到同样的错误

Error: Could not find or load main class Recursive
Caused by: java.lang.ClassNotFoundException: Recursive

递归.java

import java.util.Scanner;

public class Recursive {

    public static void main(String[] args) {
        String F1;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter any string of letters and numbers");
        F1 = input.next();
        forward(F1);
    }

    public static void forward(String f1) {
        if (f1 == null || f1.length() < 2) {
            System.out.println(f1);
            return;
        }

        System.out.println(f1.charAt(0));
        forward(f1.substring(1));
    }
}

我尝试重命名类和文件,我尝试将其加载到新项目中,我尝试了不同的在线 IDE

所以我现在有点迷失了,因为通常 IntelliJ 在出现问题时会非常有帮助

java compiler-errors
1个回答
0
投票

1。检查类名和文件名

确保您的 Java 文件名与公共类名完全匹配。根据您的错误,该文件应命名为 Recursive.java,其中的公共类应命名为 Recursive。

2。检查包裹声明

如果您的 Recursive.java 是包的一部分,请确保文件顶部的包声明与目录结构匹配。例如,如果您的文件位于 src/com/example/ 这样的文件夹路径中,则您的包声明应为 package com.example;。如果没有包声明并且它应该位于默认包中,请确保文件顶部没有此类声明。 3.在 IntelliJ 中验证项目结构

Source Folder: Make sure the folder containing your .java files is marked as a Sources Root in IntelliJ. You can check this by right-clicking on the folder and selecting "Mark Directory as" -> "Sources Root".
Build and Rebuild Project: Sometimes, IntelliJ's project index can get out of sync. Try rebuilding the project from Build -> Rebuild Project.
Classpath Settings: Check that the output directory for compiled classes (usually something like out/production/) is correctly set in IntelliJ's settings under File -> Project Structure -> Project Settings -> Modules -> Paths.

4。运行配置

确保您的运行配置正确:

Main Class: Verify that the Main Class in your run configuration is set correctly to Recursive.
Classpath of Module: Ensure that the correct module is chosen if your project has multiple modules. This should also be correctly set in the Run/Debug configurations.

5。使用终端

有时,直接从终端运行代码可以让您更清楚地了解可能出现的问题。在项目根目录中打开终端或命令提示符,然后尝试手动编译和运行 Java 文件:

重击

javac递归.java java 递归

此过程将帮助您验证问题是否不是 IntelliJ 特有的。 6.环境问题

JDK Version: Check if the JDK version used by IntelliJ is the same as the one configured in your system or terminal. Misalignment here can cause unusual behavior.
Classpath Environment Variable: Rarely, classpath problems can be due to a misconfigured CLASSPATH environment variable. You can print it in your terminal with echo $CLASSPATH (on Unix-like OS) or echo %CLASSPATH% (on Windows).

7。检查隐藏字符

如果您从网络或其他文件复制并粘贴代码,有时隐藏字符可能会导致问题。手动重新输入有问题的行或部分。 8. IDE 日志

查看 IntelliJ 的日志以获取任何线索。您可以在“帮助”->“在资源管理器/Finder 中显示日志”下找到日志。

如果这些步骤都不能解决问题,请考虑从头开始创建一个新的 IntelliJ 项目并手动添加 Java 文件。有时,项目元数据文件(如 .iml 文件或 .idea 目录)可能会损坏或配置错误。

© www.soinside.com 2019 - 2024. All rights reserved.