Error: Could not find or load main class问题解决

错误如下:

D:>java Hello
Error: Could not find or load main class Hello

代码如下:

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
    
public class Hello {
    
    private static final Logger logger = Logger.getLogger(Hello.class);
    
    public  static  void main(String argv[]) {
    	BasicConfigurator.configure();
    	logger.debug("Hello world.");
    	logger.info("What a beatiful day.");
    }
}

Java环境变量设置有问题,没有包括本目录,解决方法一:
在命令行中指定Classpath
D:>java -classpath “.;D:\apache-log4j-1.2.16\log4j-1.2.16.ja
r” Hello
0 [main] DEBUG Hello - Hello world.
1 [main] INFO Hello - What a beatiful day.

解决方法二:
在Windows的环境变量的Classpath变量的最前面加上“.;”,这种方法一劳永逸。

输出结果如下:
D:>java Hello
0 [main] DEBUG Hello - Hello world.
0 [main] INFO Hello - What a beatiful day.

Reference

What does “Could not find or load main class” mean?

你可能感兴趣的:(Java)