gpt4 book ai didi

java - 无法创建 Java 虚拟机 javaw.exe。我的世界

转载 作者:太空宇宙 更新时间:2023-11-04 09:30:17 25 4
gpt4 key购买 nike

我正在尝试使用 C# 创建一个客户端,该客户端使用一些指定启动 Minecraft 的参数来打开 javaw.exe。是的,这毫无意义,但我这样做是为了我自己的研究。使用 javaw.exe 文件的文件路径时,我似乎无法使其工作。相反,它会抛出一个错误,指出

Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.

此错误并未准确说明发生了什么。如果有日志,请告诉我,但据我谷歌研究,它可能意味着 200 多件事。

但是如果我仅使用文件路径 javaw.exe 启动它而不定义它的位置,它可以工作,但我发现它不适用于其他 java 安装。有点想知道为什么会这样,但这是一个附带问题。

我已经用谷歌搜索了有关此错误的所有内容,但发生这种情况的原因有太多,因此我计划将其放在一些代码中,以便其他人帮助我弄清楚到底发生了什么。

Java 路径函数:(我对此没有任何功劳,又名“被盗代码”)

        private string GetJavaInstallationPath()
{
string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
if (!string.IsNullOrEmpty(environmentPath))
{
return environmentPath;
}

string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}

请注意,我从下面的列表中删除了很多库,只是为了更容易阅读参数。完整代码在更下方启动 Minecraft 功能:

        private void StartMinecraft()
{
string installPath = GetJavaInstallationPath();
string filePath = System.IO.Path.Combine(installPath, "bin\\javaw.exe");
if (System.IO.File.Exists(filePath))
{
using (Process myProcess = new Process())
{
Environment.SetEnvironmentVariable("APPDATA", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+ "\\AkumaMC\\");

string userpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string libpath = userpath + "\\.AkumaMC";
string gamepath = libpath;
string assetdir = gamepath + "\\assets\\";
string accesstoken = accessToken;
string argument = "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -classpath "+ libpath + " -Djava.library.path=" + gamepath + @"\runGame -Dminecraft.launcher.brand=minecraft-launcher -Dminecraft.launcher.version=2.1.5387 -Dminecraft.client.jar=" + libpath + @"\versions\1.12.2\1.12.2.jar -cp " + libpath + @"\libraries\net\minecraftforge\forge\1.12.2-14.23.5.2838\forge-1.12.2-14.23.5.2838.jar;" + libpath + @"\libraries\net\minecraft\launchwrapper\1.12\launchwrapper-1.12.jar;" + libpath + @"\versions\1.12.2\1.12.2.jar -Xmx"+(SettingsUC.ramInt / 1024).ToString()+@"G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M -Dlog4j.configurationFile=" + libpath + @"\assets\log_configs\client-1.12.xml net.minecraft.launchwrapper.Launch --username " + uname + @" --version 1.12.2-forge --gameDir " + libpath + " --assetsDir " + libpath + @"\assets --assetIndex 1.12 --uuid " + uuidPlayer + @" --accessToken " + accesstoken + @" --userType mojang --tweakClass net.minecraftforge.fml.common.launcher.FMLTweaker --versionType Forge";

myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = filePath;
myProcess.StartInfo.Verb = "runas";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Arguments = argument;
Console.WriteLine(filePath + argument);
Console.WriteLine((SettingsUC.ramInt / 1024).ToString() + " Is the GB #");
myProcess.Start();
if (!SettingsUC.KeepLauncherOpen == true)
{
Application.Exit();
}
}

//Application.Exit();
}
else
{
MessageBox.Show("Cannot find Java installation. Please reinstall the latest version of Java and try again.", "AkumaMC Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


}

全面启动 Minecraft 功能:

        private void StartMinecraft()
{
string installPath = GetJavaInstallationPath();
string filePath = System.IO.Path.Combine(installPath, "bin\\javaw.exe");
if (System.IO.File.Exists(filePath))
{
using (Process myProcess = new Process())
{
Environment.SetEnvironmentVariable("APPDATA", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+ "\\AkumaMC\\");

string userpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string libpath = userpath + "\\.AkumaMC";
string gamepath = libpath;
string assetdir = gamepath + "\\assets\\";
string accesstoken = accessToken;
string argument = "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -classpath "+ libpath + " -Djava.library.path=" + gamepath + @"\runGame -Dminecraft.launcher.brand=minecraft-launcher -Dminecraft.launcher.version=2.1.5387 -Dminecraft.client.jar=" + libpath + @"\versions\1.12.2\1.12.2.jar -cp " + libpath + @"\libraries\net\minecraftforge\forge\1.12.2-14.23.5.2838\forge-1.12.2-14.23.5.2838.jar;" + libpath + @"\libraries\net\minecraft\launchwrapper\1.12\launchwrapper-1.12.jar;" + libpath + @"\libraries\org\ow2\asm\asm-all\5.2\asm-all-5.2.jar;" + libpath + @"\libraries\org\jline\jline\3.5.1\jline-3.5.1.jar;" + libpath + @"\libraries\net\java\dev\jna\jna\4.4.0\jna-4.4.0.jar;" + libpath + @"\libraries\com\typesafe\akka\akka-actor_2.11\2.3.3\akka-actor_2.11-2.3.3.jar;" + libpath + @"\libraries\com\typesafe\config\1.2.1\config-1.2.1.jar;" + libpath + @"\libraries\org\scala-lang\scala-actors-migration_2.11\1.1.0\scala-actors-migration_2.11-1.1.0.jar;" + libpath + @"\libraries\org\scala-lang\scala-compiler\2.11.1\scala-compiler-2.11.1.jar;" + libpath + @"\libraries\org\scala-lang\plugins\scala-continuations-library_2.11\1.0.2\scala-continuations-library_2.11-1.0.2.jar;" + libpath + @"\libraries\org\scala-lang\plugins\scala-continuations-plugin_2.11.1\1.0.2\scala-continuations-plugin_2.11.1-1.0.2.jar;" + libpath + @"\libraries\org\scala-lang\scala-library\2.11.1\scala-library-2.11.1.jar;" + libpath + @"\libraries\org\scala-lang\scala-parser-combinators_2.11\1.0.1\scala-parser-combinators_2.11-1.0.1.jar;" + libpath + @"\libraries\org\scala-lang\scala-reflect\2.11.1\scala-reflect-2.11.1.jar;" + libpath + @"\libraries\org\scala-lang\scala-swing_2.11\1.0.1\scala-swing_2.11-1.0.1.jar;" + libpath + @"\libraries\org\scala-lang\scala-xml_2.11\1.0.2\scala-xml_2.11-1.0.2.jar;" + libpath + @"\libraries\lzma\lzma\0.0.1\lzma-0.0.1.jar;" + libpath + @"\libraries\net\sf\jopt-simple\jopt-simple\5.0.3\jopt-simple-5.0.3.jar;" + libpath + @"\libraries\java3d\vecmath\1.5.2\vecmath-1.5.2.jar;" + libpath + @"\libraries\net\sf\trove4j\trove4j\3.0.3\trove4j-3.0.3.jar;" + libpath + @"\libraries\org\apache\maven\maven-artifact\3.5.3\maven-artifact-3.5.3.jar;" + libpath + @"\libraries\com\mojang\patchy\1.1\patchy-1.1.jar;" + libpath + @"\libraries\oshi-project\oshi-core\1.1\oshi-core-1.1.jar;" + libpath + @"\libraries\net\java\dev\jna\jna\4.4.0\jna-4.4.0.jar;" + libpath + @"\libraries\net\java\dev\jna\platform\3.4.0\platform-3.4.0.jar;" + libpath + @"\libraries\com\ibm\icu\icu4j-core-mojang\51.2\icu4j-core-mojang-51.2.jar;" + libpath + @"\libraries\net\sf\jopt-simple\jopt-simple\5.0.3\jopt-simple-5.0.3.jar;" + libpath + @"\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;" + libpath + @"\libraries\com\paulscode\codecwav\20101023\codecwav-20101023.jar;" + libpath + @"\libraries\com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;" + libpath + @"\libraries\com\paulscode\librarylwjglopenal\20100824\librarylwjglopenal-20100824.jar;" + libpath + @"\libraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;" + libpath + @"\libraries\io\netty\netty-all\4.1.9.Final\netty-all-4.1.9.Final.jar;" + libpath + @"\libraries\com\google\guava\guava\21.0\guava-21.0.jar;" + libpath + @"\libraries\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;" + libpath + @"\libraries\commons-io\commons-io\2.5\commons-io-2.5.jar;" + libpath + @"\libraries\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;" + libpath + @"\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;" + libpath + @"\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;" + libpath + @"\libraries\com\google\code\gson\gson\2.8.0\gson-2.8.0.jar;" + libpath + @"\libraries\com\mojang\authlib\1.5.25\authlib-1.5.25.jar;" + libpath + @"\libraries\com\mojang\realms\1.10.22\realms-1.10.22.jar;" + libpath + @"\libraries\org\apache\commons\commons-compress\1.8.1\commons-compress-1.8.1.jar;" + libpath + @"\libraries\org\apache\httpcomponents\httpclient\4.3.3\httpclient-4.3.3.jar;" + libpath + @"\libraries\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;" + libpath + @"\libraries\org\apache\httpcomponents\httpcore\4.3.2\httpcore-4.3.2.jar;" + libpath + @"\libraries\it\unimi\dsi\fastutil\7.1.0\fastutil-7.1.0.jar;" + libpath + @"\libraries\org\apache\logging\log4j\log4j-api\2.8.1\log4j-api-2.8.1.jar;" + libpath + @"\libraries\org\apache\logging\log4j\log4j-core\2.8.1\log4j-core-2.8.1.jar;" + libpath + @"\libraries\org\lwjgl\lwjgl\lwjgl\2.9.4-nightly-20150209\lwjgl-2.9.4-nightly-20150209.jar;" + libpath + @"\libraries\org\lwjgl\lwjgl\lwjgl_util\2.9.4-nightly-20150209\lwjgl_util-2.9.4-nightly-20150209.jar;" + libpath + @"\libraries\com\mojang\text2speech\1.10.3\text2speech-1.10.3.jar;" + libpath + @"\versions\1.12.2\1.12.2.jar -Xmx"+(SettingsUC.ramInt / 1024).ToString()+@"G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M -Dlog4j.configurationFile=" + libpath + @"\assets\log_configs\client-1.12.xml net.minecraft.launchwrapper.Launch --username " + uname + @" --version 1.12.2-forge --gameDir " + libpath + " --assetsDir " + libpath + @"\assets --assetIndex 1.12 --uuid " + uuidPlayer + @" --accessToken " + accesstoken + @" --userType mojang --tweakClass net.minecraftforge.fml.common.launcher.FMLTweaker --versionType Forge";

myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = filePath;
myProcess.StartInfo.Verb = "runas";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Arguments = argument;
Console.WriteLine(filePath + argument);
Console.WriteLine((SettingsUC.ramInt / 1024).ToString() + " Is the GB #");
myProcess.Start();
if (!SettingsUC.KeepLauncherOpen == true)
{
Application.Exit();
}
}

//Application.Exit();
}
else
{
MessageBox.Show("Cannot find Java installation. Please reinstall the latest version of Java and try again.", "AkumaMC Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


}

我预计它会启动我的世界,但它只是抛出一个错误。虽然如果我改变

myProcess.StartInfo.FileName = filePath;
TO
myProcess.StartInfo.FileName = "javaw.exe";

它有效,但仅适用于我的java安装(我认为)

感谢您提供的所有帮助!

更新:经过反馈,我认为这是因为它位于一个带有空格的文件夹中。那么,有没有办法完全提取 regkey 值。经过搜索,我发现 key.GetValue().ToString() 只返回第一个单词的字符串,而不是“句子”或“完整路径”。

最佳答案

看起来通过帖子的评论,我能够缩小范围并解决问题。它正在为 java 位置寻找 32 位版本的注册表。大多数人此时都使用 64 位,因此我只是添加了对操作系统的检查,然后检查了该操作系统的注册表。

如果您有兴趣自己执行此操作,我建议使用下面更新的代码,以更好地跟踪正确的 java 文件夹。

        private string GetJavaInstallationPath()
{
string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
if (!string.IsNullOrEmpty(environmentPath))
{
return environmentPath;
}
string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
if (!Environment.Is64BitOperatingSystem)
{
using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();//Not Pullying full string
}
}
}
else
{
using (var view64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry64))
{
using (var clsid64 = view64.OpenSubKey(javaKey))
{
string currentVersion = clsid64.GetValue("CurrentVersion").ToString();
using (RegistryKey key = clsid64.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();//Not Pullying full string
}
}
}
}

}

关于java - 无法创建 Java 虚拟机 javaw.exe。我的世界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159073/

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