gpt4 book ai didi

java - System.getProperty ("java.version")返回 Null

转载 作者:行者123 更新时间:2023-12-01 23:17:27 27 4
gpt4 key购买 nike

我正在尝试编译一个使用Processing 3.4源代码的类。我收到 StringIndexOutOfBoundsException当尝试编译扩展 PApplet 的类时。

PApplet类有以下代码,我认为这是问题所在

public class PApplet implements PConstants {
/** Full name of the Java version (i.e. 1.5.0_11). */
static public final String javaVersionName =
System.getProperty("java.version");

static public final int javaPlatform;
static {
String version = javaVersionName;
if (javaVersionName.startsWith("1.")) {
version = version.substring(2);
javaPlatform = parseInt(version.substring(0, version.indexOf('.')));
} else {
// Remove -xxx and .yyy from java.version (@see JEP-223)
javaPlatform = parseInt(version.replaceAll("-.*","").replaceAll("\\..*",""));
}
}

我认为字符串 javaVersionName被设置为 null 或其他一些奇怪的值,这导致 version.indexOf('.')返回-1 .

堆栈跟踪:

    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:293)
at java.lang.Thread.run (Thread.java:830)
Caused by: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 2
at java.lang.String.checkBoundsBeginEnd (String.java:3720)
at java.lang.String.substring (String.java:1909)
at processing.core.PApplet.<clinit> (PApplet.java:123)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:293)
at java.lang.Thread.run (Thread.java:830)

那里写着at processing.core.PApplet.<clinit> (PApplet.java:123)PApplet.java 中的第 123 行是我上面发布的第一行( public class PApplet ... )。

我怀疑这可能与我的路径和 JAVA_HOME 系统变量有关。我正在使用 JDK 13,它作为 C:\Program Files\Java\jdk-13\bin 添加到我的路径中并且 JAVA_HOME 变量设置为 C:\Program Files\Java\jdk-13\

编辑:我的类中的主要功能如下所示

static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "Demo" };

if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}

编辑:我还应该提到 PApplet我上面发布的代码片段不是我正在使用的实际来源,只是我在网上找到的东西。我拥有的实际来源位于 core.jar文件夹。我无法轻易改变来源。

最佳答案

这有两个方面:

  • 在上下文中抛出 StringIndexOutOfBoundsException 的事实意味着 javaVersionName 字符串不是 null ,否则,当调用 startsWith 时,您会得到一个 NullPointerException
  • 这里的问题是假设 1. 之后的字符串部分将有另一个点

Java 有多种主要版本和供应商,并且版本会根据这些版本和供应商而变化。

您应该简单地调试代码并推断那里返回的值到底是什么。

作为一个可能不相关的注释,如果(如类名所示)您的应用程序是一个小程序,您还需要小心:

Security consideration: Access to system properties can be restricted by the Security Manager. This is most often an issue in applets, which are prevented from reading some system properties, and from writing any system properties. For more on accessing system properties in applets, refer to System Properties in the Doing More With Java Rich Internet Applications lesson.

来源here

关于java - System.getProperty ("java.version")返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343940/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com