CLASSPATH 中的值的顺序重要吗?

发布于 2024-09-25 02:35:05 字数 1134 浏览 2 评论 0原文

我有 2 个单独的 java 程序 一个在 c:\test 中,另一个在 c:\test\new 中,

我可以编译它们而不会出现任何错误 \javac

但是当我尝试执行文件 \java 时 它显示这样的错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ButtonFrame.makeButton(ButtonTest3.java:42)
    at ButtonFrame.<init>(ButtonTest3.java:29)
    at ButtonTest$1.run(ButtonTest.java:17)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我把它放在我的类路径中

CLASSPATH 值 - C:\test;C:\test\new

但如果我将 CLASSPATH 中的值的顺序更改为此

CLASSPATH 值 - C:\test\new;C:\test

错误就消失了

为什么?这可能会发生 只有顺序重要吗?

I have 2 java program located seperately
One in c:\test and the other in c:\test\new

I can compile both of it without any error \javac

But when i try to execute the file \java
it shows the error like this

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ButtonFrame.makeButton(ButtonTest3.java:42)
    at ButtonFrame.<init>(ButtonTest3.java:29)
    at ButtonTest$1.run(ButtonTest.java:17)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

i put this in my classpath

CLASSPATH value- C:\test;C:\test\new

but if i change the order of the value in CLASSPATH to this

CLASSPATH value- C:\test\new;C:\test

the error is simply gone

Why?? this could happening
Only the order matters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月牙弯弯 2024-10-02 02:35:05

两个文件夹中都有一个同名的类。在 C:\test 中,有一个 ButtonTest3 类版本,其中包含导致此 NullPointerException 的编程错误。在 C:\test\new 中,有一个不同版本的 ButtonTest3 类,它不包含此错误,或者可能有一个 ButtonTest 类它所做的事情与 C:\test 中的完全不同。

清理你的类路径。在类路径中存在具有相同签名的重复的不同版本类是不好的。如果您的意图是 new 应该是包标识符,那么您需要将其远离类路径。然而,这样的包名会导致编译错误,所以不可能是这样。


至于该错误,NullPointerException 相对来说比较容易确定和修复。首先查看堆栈跟踪的第一行:

at ButtonFrame.makeButton(ButtonTest3.java:42)

它表明它发生在 ButtonTest3 类的第 42 行,在 makeButton() 方法内。现在转到 ButtonTest3.java 的第 42 行,它看起来像这样:

someObject.doSomething();

查看点运算符 . 用于调用方法或访问字段的位置某个对象。 NullPointerException 意味着 someObject 在特定时刻为 null。没有实例啊!

这是一个简单的修复:只需确保在您调用/访问它时null即可:

someObject = new SomeObject();
// ...
someObject.doSomething();

You've a class with the same name in the both folders. In C:\test there's a version of the ButtonTest3 class which contains a programming bug causing this NullPointerException. In C:\test\new there's a different version of the ButtonTest3 class which doesn't contain this bug, or probably there's a ButtonTest class which does entirely different things than the one in C:\test.

Cleanup your classpath. It's not good to have duplicate different versioned classes with the same signature in the classpath. If your intent is that new is supposed to be a package identifier, then you need to leave it away from the classpath. However, such a package name would have resulted in a compilation error, so that can't be it.


As to the bug, a NullPointerException is relatively trivial to naildown and fix. First look at the first line of the stacktrace:

at ButtonFrame.makeButton(ButtonTest3.java:42)

It's telling that it has occurred in line 42 of ButtonTest3 class, inside the makeButton() method. Now go to line 42 of ButtonTest3.java, it'll look something like:

someObject.doSomething();

Look there where a dot operator . is been used to invoke a method or access a field of some object. The NullPointerException means that someObject is null at the particular moment. There is no instance!

It's an easy fix: just ensure that it is not null at the moment you're invoking/accessing it:

someObject = new SomeObject();
// ...
someObject.doSomething();
未央 2024-10-02 02:35:05

好吧,我不相信您可以在单个源文件中定义两个类。您可以将它们定义为子类。

根据 Java 规范

每个class文件包含
单个类的定义或
界面。虽然一个班级或
接口不需要有外部
字面上包含的表示
一个文件(例如,因为
类由类加载器生成),
我们通俗地提到任何
类的有效表示或
接口位于 class 文件中
格式.格式.

您可以将 ButtonFrame 放置在 ButtonTest2 内。

public class ButtonTest2
{
    public static void main(String[] args)
    {
       ...
       ButtonFrame frame = new ButtonFrame();
    }

    class ButtonFrame extends JFrame {
       ....
   }
}

或者,将它们放在不同的 java 文件中。

Well, I don't believe you can have two classes defined in a single source file. You can have them defined as a subclass.

According to the Java spec:

Each class file contains the
definition of a single class or
interface. Although a class or
interface need not have an external
representation literally contained in
a file (for instance, because the
class is generated by a class loader),
we will colloquially refer to any
valid representation of a class or
interface as being in the class file
format.format.

You could place ButtonFrame inside ButtonTest2.

public class ButtonTest2
{
    public static void main(String[] args)
    {
       ...
       ButtonFrame frame = new ButtonFrame();
    }

    class ButtonFrame extends JFrame {
       ....
   }
}

Or, put them in different java files.

你是暖光i 2024-10-02 02:35:05

你的程序中有两个顶级课程,这是错误的。但撇开这一点不谈,你的程序一开始就没有被编译。
要成功编译程序,请使用以下 NppExec 脚本:

cmd /c cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\javac" "$(FULL_CURRENT_PATH)"
cmd /k cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\java" "$(NAME_PART)" && exit

确保将 JDK 文件夹设置为 JAVA_HOME 环境变量。
然后再试一次。

You have two classes at top level in program, thats wrong. But keeping that aside, your program is not getting compiled at first place.
To successfully compile the program use the following NppExec script:

cmd /c cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\javac" "$(FULL_CURRENT_PATH)"
cmd /k cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\java" "$(NAME_PART)" && exit

Make sure that you have your JDK folder set to JAVA_HOME environment variable.
and try again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文