mac 电脑
问题复现
随手写了一个main方法,想用命令行操作
进入 BlockDemo.java 所在目录:
wn@wangnandeMBP wn % cd /Users/wn/IdeaProjects/test/JianZhiOffer/src/main/java/com/io/wn
wn@wangnandeMBP wn % ls -l
total 16
-rw-r--r-- 1 wangnan staff 537 7 14 17:59 BlockDemo.java
drwxr-xr-x 4 wangnan staff 128 7 14 17:59 aio
drwxr-xr-x 4 wangnan staff 128 7 14 17:59 bio
drwxr-xr-x 4 wangnan staff 128 7 14 18:10 nio
编译 BlockDemo.java,生成字节码文件 BlockDemo.class
wn@wangnandeMBP wn % javac BlockDemo.java
wn@wangnandeMBP wn % ls -l
total 16
-rw-r--r-- 1 wangnan staff 823 7 14 19:23 BlockDemo.class
-rw-r--r-- 1 wangnan staff 537 7 14 17:59 BlockDemo.java
drwxr-xr-x 4 wangnan staff 128 7 14 17:59 aio
drwxr-xr-x 4 wangnan staff 128 7 14 17:59 bio
drwxr-xr-x 4 wangnan staff 128 7 14 18:10 nio
wn@wangnandeMBP wn % java BlockDemo
错误: 找不到或无法加载主类 BlockDemo
运行字节码文件 BlockDemo.class ,突然报错误: 找不到或无法加载主类 BlockDemo
wn@wangnandeMBP wn % java BlockDemo
错误: 找不到或无法加载主类 BlockDemo
解决方案:
项目的class文件是统一放到target下面,需要再target的class目录下执行运行操作才行
- 先执行compile编译,生成class文件
- 进入target的class目录下执行运行操作
wn@wangnandeMBP bio % cd /Users/wangnan/IdeaProjects/test/JianZhiOffer/target/classes
wn@wangnandeMBP classes % pwd
/Users/wangnan/IdeaProjects/test/JianZhiOffer/target/classes
wn@wangnandeMBP classes % java com.io.wn.BlockDemo
请输入一个字符串:
success
你输入的字符串是: success