2024-03-28 05:44:44 | 我爱编程网
使用下面这个PathUtil的getProgramPath()就可以获得当前程序运行的目录。
import java.net.URL;
import java.net.URLDecoder;
class PathUtil {
/**
* Get the env of windir, such as "C:\WINDOWS".
*
* @return the env of windir value.
*/
public static String getWindir() {
return System.getenv("windir");
}
/**
* Get file separator, such as "/" on unix.
*
* @return the separator of file.
*/
public static String getFileSeparator() {
return System.getProperty("file.separator");
}
/**
* Get line separator, such as "\n" on unix.
*
* @return the separator of line.
*/
public static String getLineSeparator() {
return System.getProperty("line.separator");
}
/**
* Get programPath
*
* @return programPath
*/
public static String getProgramPath() {
Class
ClassLoader loader = cls.getClassLoader();
//
// Get the full name of the class.
//
String clsName = cls.getName() + ".class";
//
// Get the package that include the class.
//
Package pack = cls.getPackage();
String path = "";
//
// Transform package name to path.
//
if (pack != null) {
String packName = pack.getName();
//
// Get the class's file name.
//
clsName = clsName.substring(packName.length() + 1);
//
// If package is simple transform package name to path directly,
// else transform package name to path by package name's
// constituent.
//
path = packName;
if (path.indexOf(".") > 0) {
path = path.replace(".", "/");
}
path = path + "/";
}
URL url = loader.getResource(path + clsName);
//
// Get path information form the instance of URL.
//
String retPath = url.getPath();
//
// Delete protocol name "file:" form path information.
//
try {我爱编程网
int pos = retPath.indexOf("file:");
if (pos > -1) {
retPath = retPath.substring(pos + 5);
}
//
// Delete the information of class file from the information of
// path.
//
pos = retPath.indexOf(path + clsName);
retPath = retPath.substring(0, pos - 1);
//
// If the class file was packageed into JAR e.g. file, delete the
// file name of the corresponding JAR e.g..
//
if (retPath.endsWith("!")) {
retPath = retPath.substring(0, retPath.lastIndexOf("/"));
}
retPath = URLDecoder.decode(retPath, "utf-8");
} catch (Exception e) {
retPath = null;
e.printStackTrace();
}
return retPath;
}
}
测试类:
public class Test{
public static void main(String args[]){
String s = PathUtil.getProgramPath();
System.out.println(s);
}
}
import java.io.File;
import java.net.URLDecoder;
public class ProjectPath {
private static String rootPath = "";
private ProjectPath() {
init();
}
@SuppressWarnings("deprecation")
private static void init() {
String path = ProjectPath.class.getResource("ProjectPath.class")
.toString();
path = URLDecoder.decode(path);
path.replaceAll("\\\\", "/");
int index = path.indexOf("WEB-INF");
if (index == -1) {
index = path.indexOf("bin");
}
if (index == -1) {
index = path.indexOf("lib");
}
if (index == -1) {
int index2 = path.indexOf("jar!");
if (index2 != -1) {
path = path.substring(0, index2);
System.out.println(path);
System.out.println(File.separator);
index = path.lastIndexOf("/");
System.out.println(index);
}
}
path = path.substring(0, index);
if (path.startsWith("jar")) {
path = path.substring(9);
}
if (path.startsWith("file")) {
path = path.substring(5);
}
if (path.endsWith("/") || path.endsWith("\\")) {
path = path.substring(0, path.length() - 1);
}
// linux环境下第一个/是需要的
String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
path = path.substring(1);
}
rootPath = path;
}
/**
* 获取应用的根目录,路径分隔符为/,路径结尾无/
*
* @return
*/
public static String getProjectPath() {
if ("".equals(rootPath)) {
init();
}
return rootPath;
}
}
我爱编程网(https://www.52biancheng.com)小编还为大家带来java运行时找不到目录的相关内容。
你这是用命令行来编译java代码?
找不到目录, 你先看看java代码在什么目录 比如在D盘 code 文件夹下有java代码HelloWorld.java
D:\code\HelloWorld.java
那么命令依次应该是
d:
cd d:\code
javac HelloWorld.java
java HelloWorld
如果还不清楚就留个联系方式帮你看看
2023-12-21 16:29:16
2023-12-24 20:21:11
2024-04-13 17:18:48
2024-04-09 17:44:01
2024-04-04 05:10:44
2024-03-31 15:14:06