Is the order of the value inside the CLASSPATH matter?

て烟熏妆下的殇ゞ 提交于 2019-12-11 08:57:09

问题


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?


回答1:


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();



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3796211/is-the-order-of-the-value-inside-the-classpath-matter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!