1、项目路径,就是指classpath的根路径了。是查找配置文件和classloader加载bytecode的起点这次就以IntelliJ IDEA为例,来聊聊项目路径,也就是classpath的事前面分享了一篇classpath和path的区别,有兴趣的tx可以去看看
2、使用Java API来查看。Code:public class ClassPathDemo { public static void main(String[柯计瓤绘] args) { String classPath = ClassPathDemo.class.getResource("/").getPath(); System.out.println("项目路径:" + classPath); }}
3、执行上述代码,看看打印的信息Output:项目路径:/E:/java/JavaStudy/out/production/JavaStudy/
4、在使用IDEA的过程,通过API查看项目路径来找编译好的class比较麻烦。这个完全可以在IDEA的配置中的找嘛下面就分享下在IDEA配置中怎么查看项目路径,也就是找到执行代码的classpath在Project 面板中点右键,在弹出的菜单中选“Open Module Settings”
5、在弹出的“Project Structure”对话框中,选中“Paths”Tab选项卡在Compiler output中,默认选择的是“Inherit project compile output path”也就是当前module使用的是Project的Compiler output路径。那么Project的Compiler output路径在哪呢?
6、点左侧的“Proje罕铞泱殳ct”选项,在右侧窗口可以看到Project的compiler output这个路径是不是和JAVA API的输入基本是一致的呢。“This directory contains tow subdirectories:Production and Test for production code and test sources,respectively.”