输入重定向到 Java - 无法找到或加载主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42027117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:52:43  来源:igfitidea点击:

Input redirection into Java - Could not find or load main class

javaintellij-idea

提问by Ahmad Fahmy

I tried every online solution and i still get the same result..i'm trying to get my code to run on a file "distinct.txt" and here is my attempt to run it.

我尝试了所有在线解决方案,但仍然得到相同的结果..我正在尝试让我的代码在文件“distinct.txt”上运行,这是我运行它的尝试。

here is my try

这是我的尝试

i tried different paths and all give same result.

我尝试了不同的路径,都给出了相同的结果。

here is my code

这是我的代码

package pset2.sol;
import edu.princeton.cs.algs4.StdIn;
public class Permutation {
public static void main(String[] args){
    RandomizedQueue<String> rq = new RandomizedQueue<>();
    while (!StdIn.isEmpty()) {
        String item = StdIn.readString();
        rq.enqueue(item);
    }
    int k = Integer.parseInt(args[0]);
    for(int i = 0; i < k; i++){
        System.out.println(rq.dequeue());
    }
}
}

回答by matejetz

My guess is that your source-folder is not set correctly.

我的猜测是您的源文件夹设置不正确。

Try File -> Project Structure -> Modules -> (tab) Sources -> Mark as: Sources.

尝试文件 -> 项目结构 -> 模块 ->(选项卡)来源 -> 标记为:来源。

That fixed the problem for me after importing an Eclipse Project.

导入 Eclipse 项目后,这为我解决了问题。

回答by OneCricketeer

Ideally, you should always use the green button from IntellIJ. You can set the Edit Configurations to give the path to the file. I don't know about reading in the file from stdin through there, though.

理想情况下,您应该始终使用 IntellIJ 的绿色按钮。您可以设置编辑配置以提供文件的路径。不过,我不知道如何从 stdin 读取文件。



Your problem is not IntelliJ, though, it's the CMD.

不过,您的问题不是 IntelliJ,而是 CMD。

First, make sure you are in the right folder.

首先,确保您位于正确的文件夹中。

Run cd /path/to/compiled_filesto see that you are at the parent of the psetfolder.

运行cd /path/to/compiled_files以查看您位于pset文件夹的父级。

pset
   /sol2
      Permutation.class

Then, you'll likely need a classpath to have the other classes within the pset.sol2package to be resolved.

然后,您可能需要一个类路径来pset.sol2解析包中的其他类。

java -cp . pset.sol2.Permutation

More details: What does "Could not find or load main class" mean?

更多细节:“找不到或无法加载主类”是什么意思?

回答by isapir

You have to make sure that the "Project compiler output" value is set, in Project Structure > Project, and that the module's Compiler output points to a valid path:

您必须确保在 中设置了“项目编译器输出”值Project Structure > Project,并且模块的编译器输出指向有效路径:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Jinglesting

A bit late to the party here, but I found that my Main file (in the source file), was missing the ".java" extension, even though the file looked fine in the intelliJ project explorer.

在这里聚会有点晚,但我发现我的主文件(在源文件中)缺少“.java”扩展名,即使该文件在 intelliJ 项目资源管理器中看起来不错。

However on close inspection, I noticed that the file type in /out/ was not a class file (i.e. did not have the blue circle with a "C" in).

但是仔细检查后,我注意到 /out/ 中的文件类型不是类文件(即没有带有“C”的蓝色圆圈)。

I think this could be because I created the "main" file in my source folder, before I marked that folder as source in the project settings, and the file type created was undefined somehow...

我认为这可能是因为我在我的源文件夹中创建了“主”文件,然后我在项目设置中将该文件夹标记为源,并且创建的文件类型以某种方式未定义......

To fix, I renamed the Main file to Main.java (in finder), and rebuilt the project from the build menu.

为了修复,我将 Main 文件重命名为 Main.java(在 finder 中),并从构建菜单重建项目。

回答by Badamchi

In intellij, choose Run. | Edit Configurations from the main menu.

在 Intellij 中,选择运行。| 从主菜单编辑配置。

enter image description here

在此处输入图片说明