简体   繁体   English

在Mac上运行Java的问题

[英]Problems in running java on mac

I am using a Macbook Pro, Maverics OS, 64 bit machine. 我正在使用Macbook Pro,Maverics OS,64位计算机。
Actually I am facing a very itchy problem while running java programs on my computer. 实际上,在计算机上运行Java程序时,我遇到了一个非常痒的问题。

For example, this is my code: 例如,这是我的代码:

package com.gui.helloworld;



import javax.swing.JOptionPane;



/**
 * <p>Hello World popup</p>
 * This class simply pops up a "Hello, World!"
 * message graphically.
 *
 * @author Aditya R.Singh
 * @version 1.0
 * @since 2014-06-22
 */
public class GUIHelloWorld {

    /**
     * This is the method that will popup the 
     * "Hello, World!" message.
     * @param args Unused
     */
    public static void main(String[] args) {

        JOptionPane.showMessageDialog(null, "Hello, World!");
    }
}

When I compile my code like this, 当我这样编译代码时,

javac com/gui/helloworld/GUIHelloWorld.java  

a GUIHelloWorld.class is produced. 生成一个GUIHelloWorld.class。
Upto this place, it is fine. 到这个地方,很好。

BUT THE PROBLEM ARISES HERE 但是这里出现了问题

When I try to run the code like this: 当我尝试运行这样的代码时:

java com.gui.helloworld.GUIHelloWorld    

It gives me an error like 它给我一个错误

Error: Could not find or load main class com.gui.helloworld.GUIHelloWorld 

So I have to compile the program like this: 所以我必须像这样编译程序:

java -classpath . com.gui.helloworld.GUIHelloWorld  

Now, the output appears. 现在,出现输出。
That's fine, but still, why is the need to put that "classpath" when the .class file is in 很好,但是,为什么仍然需要在.class文件中放入“ classpath”
the same directory? 同一目录?
I am not liking this style. 我不喜欢这种风格。
This wasn't a problem when I used to use a windows 7 machine two months ago. 两个月前我以前使用Windows 7计算机时,这不是问题。
Any help? 有什么帮助吗? Or is it so that it is necessary for mac OS? 还是对Mac OS而言是必要的?
Please help. 请帮忙。

Thanks, in advance ;) 提前致谢 ;)

javac or java without a classpath option have the working directory as the default classpath. 不带classpath选项的javac或java将工作目录作为默认类路径。

References to classes must include package names. 对类的引用必须包含程序包名称。 So if you compile and execute 因此,如果您编译执行

javac One.java
java One

class One should be in the default package (no package statement). class One应该在默认包中(没有package语句)。 If you compile 如果编译

javac a/b/Two.java

class Two should be in the package ab Then, execution of the .class file a/b/Two.class is possible using class 2应该放在包ab中。然后,可以使用以下命令执行.class文件a / b / Two.class

java a.b.Two

Other "effects" are possible by setting the environment variable CLASSPATH, which can be overridden by the -cp option. 通过设置环境变量CLASSPATH可以实现其他“效果”,该变量可以被-cp选项覆盖。 (Possibly this is what happened in your case - check by running echo $CLASSPATH .) (这可能是您的情况发生的情况-通过运行echo $CLASSPATH检查。)

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

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