java获取项目路径

作者:烟中隐约闪现 | 创建时间: 2023-05-15
Java中项目路径,就是classpath了。 本文结合IntelliJ IDEA分享一下对classpath的理解了...
java获取项目路径

操作方法

项目路径,就是指classpath的根路径了。 是查找配置文件和classloader加载bytecode的起点 这次就以IntelliJ IDEA为例,来聊聊项目路径,也就是classpath的事 前面分享了一篇classpath和path的区别,有兴趣的tx可以去看看

使用Java API来查看。 Code: public class ClassPathDemo { public static void main(String[] args) { String classPath = ClassPathDemo.class.getResource("/").getPath();               System.out.println("项目路径:" + classPath); } }

执行上述代码,看看打印的信息 Output: 项目路径:/E:/java/JavaStudy/out/production/JavaStudy/

在使用IDEA的过程,通过API查看项目路径来找编译好的class比较麻烦。 这个完全可以在IDEA的配置中的找嘛 下面就分享下在IDEA配置中怎么查看项目路径,也就是找到执行代码的classpath 在Project 面板中点右键,在弹出的菜单中选“Open Module Settings”

在弹出的“Project  Structure”对话框中,选中“Paths”Tab选项卡 在Compiler output中,默认选择的是“Inherit project compile output path” 也就是当前module使用的是Project的Compiler output路径。 那么Project的Compiler output路径在哪呢?

点左侧的“Project”选项,在右侧窗口可以看到Project的compiler output 这个路径是不是和JAVA API的输入基本是一致的呢。 “This directory contains tow subdirectories: Productionand Test for production code and test sources,respectively.”

点击展开全文

更多推荐