繁体   English   中英

无法在同一个包中的编译类中看到静态方法

[英]Cant see static methods in compiled classes in same package

我想为调试模式定义一个带有静态标志的类,我的程序中的对象可以看到并相应地输出调试信息。 “Option”类没有依赖项,我先编译它。 (全部来自命令行/.bat 文件) Option 类编译,但引用它的类不会编译,并出现“找不到符号”错误。

//Options - holds options for code, including debug modes, getters, setters
package UserViewer;

public class Option {

    private static boolean debug1 = false;
    private static boolean debug2 = false;
    private static boolean debug3 = false;
    private static boolean debug4 = false;

    public static boolean isDebug1() {
        return debug1;
    }
    public static void setDebug1(boolean val) {
        debug1 = val;
    }
    public static boolean isDebug2() {
        return debug2;
    }
    public static void setDebug2(boolean val) {
        debug2 = val;
    }
    public static boolean isDebug3() {
        return debug3;
    }
    public static void setDebug3(boolean val) {
        debug3 = val;
    }
    public static boolean isDebug4() {
        return debug4;
    }
    public static void setDebug4(boolean val) {
        debug4 = val;
    }
}

这是我尝试编译的第一个类,它引用了 Option 中的静态方法

//contains all the User Interface elements
package UserViewer;

import javafx.scene.layout.BorderPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;

class UIPane extends BorderPane implements EventHandler<ActionEvent> {

    UIPane() {
        if(Option.isDebug1() == true) { System.out.println("UIPane succesfully loaded."); }
    }

    @Override
    public void handle(ActionEvent e) {
        if(Option.isDebug2() == true) { System.out.println("Action Event detected in UIPane."); }
    }
}

这里是compile.bat

javac -d . CONST.java
javac -d . Option.java
javac -d . UIPane.java
javac -d . App.java
javac -d . UserViewer.java
java UserViewer.UserViewer DEBUG1
pause

由于它们都在同一个包中 - UserViewer - 我想我可以从程序的任何地方引用这些静态方法。

这是(第一个)编译器错误:

C:\...\UserViewer>javac -d . UIPane.java
UIPane.java:12: error: cannot find symbol
    if(Option.isDebug1() == true) { System.out.println("UIPane succesfully loaded."); }
 symbol:   variable Option
 location: class UIPane
UIPane.java:17: error: cannot find symbol
    if(Option.isDebug2() == true) { System.out.println("Action Event detected in UIPane."); }
 symbol: variable Option
 location: class UIPane

我对 CONST.java 和所有其他依赖于 CONST 和 Option 的对象有同样的问题,但我省略了其他错误,因为它们似乎都是相同的问题 - 我的其余代码看不到 Option.class 或 CONST .class,虽然它们在同一个包中。 Option.class 和 CONST.class 会编译并位于正确的包子目录 UserViewer 中。

任何帮助表示赞赏。

编辑:评论中的解决方案

我需要同时编译所有类或为依赖类声明类路径。

我的新 .bat 文件如下所示:

javac -d . *.java
java -cp ./UserViewer;. UserViewer.UserViewer DEBUG1

正如评论中指出的那样,您必须一口气编译所有类。

这些是执行此操作的步骤:

  1. 在控制台中使用cd导航到主类的目录。
  2. 输入javac NameOfYourMainClass.java并按回车键。

一个例子:

想象一下这是你的文件系统:

  • 桌面
    • 我的项目
      • 主要的
        • MainClass.java <-- 包含main(String[] args)
      • 调试
        • 选项.java

现在假设myProject文件夹是您项目的根目录,您将使用控制台将工作目录更改为main ,然后编写javac MainClass.java

如果您想在之后运行您的程序,请输入java NameOfYourMainClass

暂无
暂无

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

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