System.getenv() 返回 \

发布于 2024-12-25 17:01:57 字数 944 浏览 1 评论 0原文

我正在尝试使用 System.getenv 函数创建到计算机上某个位置的路径,它在路径中返回 \ 而不是我需要的 / 。我尝试过使用 ReplaceAll 方法,但它返回一个错误:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at Launcher.start(Launcher.java:75)
    at Launcher.Download(Launcher.java:55)
    at Launcher.<init>(Launcher.java:31)
    at Launcher.main(Launcher.java:17)

代码行是:

InputStream OS = Runtime.getRuntime().exec(new String[]{"java",System.getenv("APPDATA").replaceAll("\\", "/")+"/MS2-torsteinv/MS2-bin/no/torsteinv/MarsSettlement2/Client/Client.class"}).getErrorStream();

I'm trying to make a path to a place on the computer with the System.getenv function and it returns a \ in the path not a / which is what I need. I have tried with the replaceAll method but it returns a error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at Launcher.start(Launcher.java:75)
    at Launcher.Download(Launcher.java:55)
    at Launcher.<init>(Launcher.java:31)
    at Launcher.main(Launcher.java:17)

the line of code is:

InputStream OS = Runtime.getRuntime().exec(new String[]{"java",System.getenv("APPDATA").replaceAll("\\", "/")+"/MS2-torsteinv/MS2-bin/no/torsteinv/MarsSettlement2/Client/Client.class"}).getErrorStream();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹎☆浅夏丿初晴 2025-01-01 17:01:57

您需要加倍反斜杠:

.replaceAll("\\\\", "/")

规范的正则表达式确实是 \\,但在 Java 正则表达式中位于字符串中,而在 Java 字符串中,需要用另一个反斜杠转义文字反斜杠。因此 \\ 变为 "\\\\"

You need to double the backslash:

.replaceAll("\\\\", "/")

The canonical regex is indeed \\, but in Java regexes are in strings, and in Java strings, a literal backslash needs to be escaped with another backslash. Hence \\ becomes "\\\\".

羁绊已千年 2025-01-01 17:01:57

在 Java 正则表达式中,您必须再次转义反斜杠,而在 Java 字符串中则必须再次转义。这总共有四个反斜杠。

replaceAll("\\\\", "/")

In Java regular expressions you have to escape the backslash and in java string again. That makes in total four backslashes.

replaceAll("\\\\", "/")
み青杉依旧 2025-01-01 17:01:57

它在路径中返回一个 \ 而不是我需要的 / 。

平台默认设置肯定就是您所需要的。

import java.io.File;

class FormPath {
    public static void main(String[] args) {
        String relPath = "/MS2-torsteinv/MS2-bin/no/" +
            "torsteinv/MarsSettlement2/Client/Client.class";
        String[] parts = relPath.split("/");
        File f = new File(System.getenv("APPDATA"));
        System.out.println(f + " exists: " + f.exists());

        for (String part : parts) {
            // use the File constructor that will insert the correct separator
            f = new File(f,part);
        }
        System.out.println(f + " exists: " + f.exists());
    }
}

输出

C:\Users\Andrew\AppData\Roaming exists: true
C:\Users\Andrew\AppData\Roaming\MS2-torsteinv\MS2-bin\no\torsteinv\MarsSettlement2\Client\Client.class exists: false
Press any key to continue . . .

it returns a \ in the path not a / wich is what i need.

The platform default sure is what you need.

import java.io.File;

class FormPath {
    public static void main(String[] args) {
        String relPath = "/MS2-torsteinv/MS2-bin/no/" +
            "torsteinv/MarsSettlement2/Client/Client.class";
        String[] parts = relPath.split("/");
        File f = new File(System.getenv("APPDATA"));
        System.out.println(f + " exists: " + f.exists());

        for (String part : parts) {
            // use the File constructor that will insert the correct separator
            f = new File(f,part);
        }
        System.out.println(f + " exists: " + f.exists());
    }
}

Output

C:\Users\Andrew\AppData\Roaming exists: true
C:\Users\Andrew\AppData\Roaming\MS2-torsteinv\MS2-bin\no\torsteinv\MarsSettlement2\Client\Client.class exists: false
Press any key to continue . . .
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文