用Java获取当前工作目录
我想使用以下命令访问我当前的工作目录
String current = new java.io.File( "." ).getCanonicalPath();System.out.println("Current dir:"+current);String currentDir = System.getProperty("user.dir");System.out.println("Current dir using System:" +currentDir);
输出:
Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32
我的输出不正确,因为C驱动器不是我的当前目录。 在这方面需要帮助。
#1楼
我希望您要访问当前目录,包括该软件包,即,如果您的Java程序位于c:\\myApp\\com\\foo\\src\\service\\MyTest.java
并且要打印到c:\\myApp\\com\\foo\\src\\service
然后您可以尝试以下代码:
String myCurrentDir = System.getProperty("user.dir")+ File.separator+ System.getProperty("sun.java.command").substring(0, System.getProperty("sun.java.command").lastIndexOf(".")).replace(".", File.separator);System.out.println(myCurrentDir);
注意:此代码仅在带有Oracle JRE的Windows中经过测试。
#2楼
请参阅: http : //docs.oracle.com/javase/tutorial/essential/io/pathOps.html
使用java.nio.file.Path
和java.nio.file.Paths
,您可以执行以下操作以显示Java认为您当前的路径。 这适用于7及以后,并使用NIO。
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);
输出的Current relative path is: /Users/george/NetBeansProjects/Tutorials
(在我的情况下,是我从中运行该类的位置)。 通过不使用前导分隔符来指示您正在构造绝对路径,以相对方式构造路径将以该相对路径为起点。
#3楼
this.getClass().getClassLoader().getResource("").getPath()
#4楼
这是我的解决方案
File currentDir = new File("");
#5楼
这是当前目录名称
String path="/home/prasad/Desktop/folderName";
File folder = new File(path);
String folderName=folder.getAbsoluteFile().getName();
这是当前目录路径
String path=folder.getPath();
#6楼
我在Linux上,这两种方法都得到相同的结果:
@Test
public void aaa()
{System.err.println(Paths.get("").toAbsolutePath().toString());System.err.println(System.getProperty("user.dir"));
}
Paths.get("")
文档
System.getProperty("user.dir")
文档
#7楼
使用CodeSource#getLocation()
。
这在JAR文件中也可以正常工作。 您可以通过ProtectionDomain#getCodeSource()
获取CodeSource
,然后可以通过Class#getProtectionDomain()
获取ProtectionDomain
。
public class Test {public static void main(String... args) throws Exception {URL location = Test.class.getProtectionDomain().getCodeSource().getLocation();System.out.println(location.getFile());}
}
#8楼
以下内容适用于Java 7及更高版本(请参见此处以获取文档)。
import java.nio.file.Paths;Paths.get(".").toAbsolutePath().normalize().toString();
#9楼
System.getProperty("java.class.path")
#10楼
通常,作为File对象:
File getCwd() {return new File("").getAbsoluteFile();
}
您可能希望具有完整的合格字符串,例如“ D:/ a / b / c”,它是:
getCwd().getAbsolutePath()
#11楼
这里发布的答案都没有对我有用。 这是起作用的内容:
java.nio.file.Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()
);
编辑:我的代码中的最终版本:
URL myURL = getClass().getProtectionDomain().getCodeSource().getLocation();
java.net.URI myURI = null;
try {myURI = myURL.toURI();
} catch (URISyntaxException e1)
{}
return java.nio.file.Paths.get(myURI).toFile().toString()
#12楼
假设您正在尝试在eclipse,netbean或命令行中单独运行项目。 我已经写了一种解决方法
public static final String getBasePathForClass(Class<?> clazz) {File file;try {String basePath = null;file = new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());if (file.isFile() || file.getPath().endsWith(".jar") || file.getPath().endsWith(".zip")) {basePath = file.getParent();} else {basePath = file.getPath();}// fix to run inside eclipseif (basePath.endsWith(File.separator + "lib") || basePath.endsWith(File.separator + "bin")|| basePath.endsWith("bin" + File.separator) || basePath.endsWith("lib" + File.separator)) {basePath = basePath.substring(0, basePath.length() - 4);}// fix to run inside netbeanif (basePath.endsWith(File.separator + "build" + File.separator + "classes")) {basePath = basePath.substring(0, basePath.length() - 14);}// end fixif (!basePath.endsWith(File.separator)) {basePath = basePath + File.separator;}return basePath;} catch (URISyntaxException e) {throw new RuntimeException("Cannot firgue out base path for class: " + clazz.getName());}
}
使用时,在任何要获取文件基本路径的地方,都可以将锚类传递给上述方法,结果可能是您需要的东西:D
最好,
#13楼
在Linux上,当您从terminal运行jar文件时,无论您的jar文件在哪里,它们都将返回相同的String
: “ / home / CurrentUser” 。 启动jar文件时,这取决于终端使用的当前目录。
Paths.get("").toAbsolutePath().toString();System.getProperty("user.dir");
如果将具有main
的Class
称为MainClass
,请尝试:
MainClass.class.getProtectionDomain().getCodeSource().getLocation().getFile();
这将返回带有jar文件绝对路径的String
。
#14楼
使用Windows user.dir返回预期的目录,但是以提升的权限启动应用程序(以管理员身份运行)时不会返回该目录,在这种情况下,您会得到C:\\ WINDOWS \\ system32
#15楼
提及它仅在Windows
进行检查,但我认为它在其他操作系统[ Linux,MacOs,Solaris
] :)上运行良好。
我在同一目录中有2个 .jar
文件。 我想从一个.jar
文件启动同一目录中的另一个.jar
文件。
问题是,当您从cmd
启动它时,当前目录是system32
。
警告!
- 即使在使用文件夹名称的情况下,以下代码在所有测试中也能很好地工作
;][[;'57f2g34g87-8+9-09!2#@!$%^^&()
或()%&$%^@#
效果很好。 - 我将
ProcessBuilder
与以下内容一起使用:
用Java获取当前工作目录相关推荐
- java获取当前工作目录
java获取当前工作目录 代码片段 System.out.println("用户的当前工作目录:/n"+System.getProperty("user.dir" ...
- python工作目录_python获取当前工作目录
在编程中经常需要用到当前脚本的工作目录,以下是python脚本获取当前工作目录的语句.例test.py importinspect dir = inspect.getfile(inspect.curr ...
- Linux中 shell 脚本获取当前工作目录的方法
Linux中 shell 脚本获取当前工作目录的方法 1. 示例 2. 注意: 参考: 1. 示例 test.sh target_dir=$(cd $(dirname $0); pwd) echo $ ...
- java获取远程服务器目录,在远程服务器创建三级目录
java获取远程服务器目录,在远程服务器创建三级目录 1.添加依赖 <dependency><groupId>com.jcraft</groupId><art ...
- java获取默认用户目录_Java获取当前路径
1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...
- VC 获取当前工作目录和执行目录的一些方法
1.System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 获取模块的完整路径. 2. System.Env ...
- Python获取当前工作目录
1.sys.arg[0]: import sys print(sys.argv[0])#当前脚本的位置 1 2 输出结果: G:/Pythonxx/test.py 1 2.os模块 impor ...
- 在python中获取当前工作目录可以通过_python-获取当前工作路径
1.sys.argv[0] import sys print sys.argv[0]#获得的是当前执行脚本的位置(若在命令行执行的该命令,则为空) 运行结果(在python脚本中执行的结果): F:/ ...
- java获取WEB INF目录绝对路径
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! JSP ...
最新文章
- android 内核 netlink 上报,Network Daemon(Android Netd)架构和源码分析
- 获取打开文件的路径和文件名
- 2010.07.13_19:30
- java 根据圆心计算圆弧上点的经纬度_【控制测量学】-高斯投影正算公式以及java代码
- 史上最全ClassLoader总结
- 轨迹规划实现 tfaar_example2.7
- Python+OpenCV:ORB: An efficient alternative to SIFT or SURF
- 一种数字全息散斑干涉测量仿真模拟系统
- ios刷机固件下载网址
- 手把手教你使用VSS
- 20款知名PHP集成环境推荐与优缺点分析、php环境大全推荐(PHP环境搭建包)
- html中设置字体字号,html中设置字体大小的方法
- 数学三次方的计算机符号,数学符号三次方
- Android 项目必备(二)--> 启动页 引导页
- 一个很好用的Venn图在线编辑网站
- python饿了么商家开放平台模拟网页http请求实战
- Google新的搜索页面
- 2020 社招 JAVA面试题总结
- 不锈钢常识 - Powered by Discuz!
- Excel 2007版的常用功能(1):Excel基本操作
热门文章
- Python Mysql 数据库操作
- WebGIS中利用AGS JS+eCharts实现一些数据展示的探索
- 设置sqlplus 显示列数和行数
- C#—接口和抽象类的区别?
- leaflet.toolbar.js
- 潭州教育-Python学习笔记@基本数据类型:元祖字典
- 我觉得这个世界不是特别的好,你说呢
- svn 设置文件可执行权限
- linux下 mysql5.5数据库迁移操作
- ExcelToHtmlTable转换算法:将Excel转换成Html表格并展示(项目源码+详细注释+项目截图)...