java获取项目路径的方法:
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.点左侧的“project”选项,在右侧窗口可以看到project的compiler output
这个路径是不是和java api的输入基本是一致的呢。
“this directory contains tow subdirectories:production and test for production code and test sources,respectively.”
推荐教程:《java视频教程》
以上就是java如何获取项目路径?的详细内容。