简体   繁体   English

当我从命令行运行 Main 时,Java 在同一个 package 中找不到 class

[英]Java can't find class in same package when I run Main from command line

I moved a class from Main.java into their own.java files and now the IDE (IntelliJ) can't find them, even though they are in the same package. Here are the first lines of Main...我将 class 从 Main.java 移到他们自己的 .java 文件中,现在 IDE (IntelliJ) 找不到它们,即使它们在同一个 package 中。这是 Main 的第一行...

package readability;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        var filePath = args[0];
        var textAnalyser = new TextAnalyser(filePath);

... and here's the error I get when I run it from the command line: ...这是我从命令行运行它时遇到的错误:

C:\Users\123md\IdeaProjects\Readability Score\Readability Score\task\src\readability>java Main.java in.txt
Main.java:8: error: cannot find symbol
        var textAnalyser = new TextAnalyser(filePath);
                               ^
  symbol:   class TextAnalyser
  location: class Main

Interestingly, when I just say String filePath = "in.txt" and run it in the console, it does find the class and runs fine, so why can't it find the class when I run it from the command line?有趣的是,当我只是说String filePath = "in.txt"并在控制台中运行它时,它确实找到了 class 并且运行正常,那么为什么当我从命令行运行它时它找不到 class? Thanks!谢谢!

You can only run single *.java file with java command.您只能使用java命令运行单个*.java文件。 If your program uses mutiple files, you have to compile it first:如果你的程序使用多个文件,你必须先编译它:

javac readability/Main.java readability/TextAnalyser.java

and then run:然后运行:

java readability.Main in.txt

For more info see: https://openjdk.java.net/jeps/330有关详细信息,请参阅: https://openjdk.java.net/jeps/330

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

相关问题 从命令行运行Java-无法找到主类 - Run Java from the command line — unable to find main class Java从命令行运行jar:找不到或加载主类com.test.Main时出错 - Java run jar from command line: Error could not find or load main class com.test.Main 尝试在命令行上运行时,Java“无法找到或加载主类” - Java “cannot find or load main class” when trying to run on command line 从Gradle执行Java类时找不到Main - Can't find Main when executing a Java class from Gradle 尝试从命令行运行时,Maven 找不到主 class - Maven cannot find main class when trying to run from command line Java Jar 文件主类在从命令行运行时运行,而不是从 Windows 批处理文件运行 - Java Jar file main class run when runs from command line, but not from Windows batch file 如何使用Unix命令行运行java包? - how can I run java package using the Unix command line? 为什么sbt不能在Java中找到运行任务的主类? - Why can't sbt find main class in Java for run task? Java:如果不包含软件包,则可以在命令提示符下运行,如果包含软件包,则找不到类错误 - Java: I can run in command prompt if I don't include a package, if I do include a package I get no class found errors 一个Java类在同一包中找不到另一个 - A java class can't find an other in the same package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM