AS3 - 获取 JAVA Home 路径 [英] AS3 - Get JAVA Home path

查看:35
本文介绍了AS3 - 获取 JAVA Home 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Java Home lib 目录中复制一个扩展.但我的问题是在 Windows 和 Mac OS 上找到 java home.每次 idk 更新时,此目录都会更改.你能帮忙用环境变量修复它吗?最好的问候.

I need to copy an extension on Java Home lib directory. But my problem is to find java home on Windows and on Mac OS. This directory change on each idk update. Could you help to fix it with environnement variables. Best regards.

推荐答案

你能帮忙用环境变量修复它吗?

您能否确定您的应用用户实际上已将JAVA_HOME 路径添加到其帐户的环境设置中?如果他们没有遵循一些教程,那么您的想法将一无所获.

Can you be sure that your app user has actually added JAVA_HOME path into their account's Environment settings? If they didn't follow some tutorial then your idea will find nothing.

但我的问题是在 Windows 和 Mac OS 上找到 JAVA_HOME.

But my problem is to find JAVA_HOME on Windows and on Mac OS.

您可以使用终端命令查找JAVA_HOME.

You can use terminal commands to find JAVA_HOME.

Mac OS:(来源:未经测试,因为没有可用的 Mac)

Mac OS: (source : untested since no Mac available)

/usr/libexec/java_home


Windows 操作系统:(使用 cmd.exe 命令行)

选项 1 :通常 set 添加新条目进入环境变量,但如果不带参数使用它会只需列出现有的环境变量.

Option 1 : Normally set adds new entry into Environment Variables, but if used without parameters it will just list the existing Environment Variables.

set

这给了我长文本的结果,包括:

which gives me result of long text including :

JAVA_HOME=C:\Program Files\Java\jre1.8.0_102
JDK_HOME=C:\Program Files\Java\jdk1.8.0_102
JRE_HOME=C:\Program Files\Java\jre1.8.0_102\jre

选项 2 :(获取系统当前的 Java.exe 路径)

for %i in (java.exe) do @echo.   %~$PATH:i

这给了我一个结果:C:\Program Files\Java\jre1.8.0_102\bin\java.exe

选项 3 :对于 Windows,我认为最好的选择是检查 Windows 注册表 以获取用户的 currentVersion 和然后检查该特定当前版本的 JavaHome.

Option 3 : For Windows I think the best option is to check Windows registry for user's currentVersion and then check the JavaHome of that specific currentVersion.

(a) 检查 Windows 使用的 Java 当前版本...

(a) Check for Java current version used by Windows...

REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\ /v "CurrentVersion" /s

注意:或者只是执行 java -version 以获得下一步 (b) 的正确数字.

note : Or just do java -version to get the correct number for next step (b).

(b) 根据上面找到的版本(例如:1.8)检查 Java 主路径1.8.0_102)...

(b) Check for Java Home path based on the above found version (eg: 1.8 or 1.8.0_102)...

REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\ /v "JavaHome" /s


cmd.exe 窗口中练习上述选项以轻松查看其输出.

Practice the above options in the cmd.exe window to see their output easily.

稍后,在您的 AS3 代码中,您可以使用 Regular ExpressionString 函数提取您需要的数字或目录路径.

Later, in your AS3 code, you can use Regular Expression or String functions extract the number or directory path that you need.

Options ++ : 其他选项包括使用 where/rc:\ java.exe 搜索 c:\ 驱动器用于文件位置(文件夹),但在大型驱动器上可能会很慢(是 aphabetical 并搜索所有子文件夹).

Options ++ : Other options include using where /r c:\ java.exe to search c:\ drive for the file location (folder) but can be slow on large drives (is aphabetical and searches all sub-folders).

您询问的是环境变量(不是注册表),因此下面是一个快速示例,可以在 Windows 中执行选项 1.

You asked about Environment Variables (not Registry) so below is a quick example to do that Option 1 for Windows.

(PS:如果您在 IDE 中进行测试时收到关于不支持本机进程"的错误信息,请在您的 AIR 设置中勾选扩展桌面",然后稍后对于导出的运行时版本,您必须取消勾选,现在只勾选桌面").

(PS: if you get an error about "Native Process is not supported" when testing in IDE you tick only "Extended Desktop" in your AIR settings, then later for exported runtime version you must un-tick that and now tick only "Desktop").

Windows 的可测试代码示例,它静默打开 CMD.exe 并通过 set 命令,然后接收其返回的文本输出.代码可以由你缩短...

Example testable code for Windows which silently opens CMD.exe and passes set command, then receives its returned text output. The code can be shortened by you...

package {

import flash.display.MovieClip;

import flash.events.*;
import flash.errors.*;

import flash.desktop.NativeProcess;
import flash.desktop.*;

import flash.filesystem.File;

import flash.utils.ByteArray;


public class Java_Get_Path_v3 extends MovieClip 
{

    private var NProcessExe:File;

    private var NProcess :NativeProcess; //external Process (eg: notepad.exe)
    private var nativeProcessStartupInfo :NativeProcessStartupInfo;
    private var NProcess_Args :Vector.<String> = new Vector.<String>(); //Process startup options

    private var str_CMD_Output :String = "";

    private var str_Java_Home :String = "";
    private var str_JDK_Home :String = "";
    private var str_JRE_Home :String = "";

    private var temp_Int :int = -1;


    public function Java_Get_Path_v3() 
    {
        //# 1) setup Native Process (Command Line runs in background (no window) )
        createNativeProcess();

        //# 2) get Java "Home" path from Windows via "Environment Variables";
        get_Java_Path(); //is via stdout

    }


    private function createNativeProcess():void 
    {
        if (NativeProcess.isSupported) 
        {
            trace("## doing FUNCTION : createNativeProcess()" );

            var pathToEXE:String = "c:/windows/system32/cmd.exe";
            NProcessExe = File.applicationDirectory.resolvePath(pathToEXE);

            if (NProcessExe.exists)
            {
                //# initialise Process
                NProcess = new NativeProcess();

                //# add Process listeners for events
                NProcess.addEventListener(ProgressEvent.PROGRESS, STD_onOutputData); 
                NProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, STD_onOutputData); 
                NProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, STD_onErrorData); 

                NProcess.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, STD_onIOError);
                NProcess.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, STD_onIOError);
            } 
            else 
            { trace("The specified .exe file was not found"); }

        } 
        else //# if  NOT suppoNativeProcess.
        {
            trace("Native Process not supported in this mode (use only \"Extended Desktop\" when debugging)");
        }
    }


    private function get_Java_Path():void 
    {
        //# use to get Java Home path
        NProcess_Args[0] = ("\/c" + "set");

        //# use to get Java version
        //NProcess_Args[0] = ("\/c" + "java -version");

        //# start the Native Process (eg: silent running of CMD.exe)
        //# triggers :::: public function STD_onOutputData
        nativeProcessStartupInfo = new NativeProcessStartupInfo();
        nativeProcessStartupInfo.executable = NProcessExe;
        nativeProcessStartupInfo.arguments = NProcess_Args;

        NProcess.start(nativeProcessStartupInfo);

    }


    private function update_String_with_Path():void 
    {
        trace("## doing FUNCTION : update_String_with_Path()" );

        //trace("str_CMD_Output : " + str_CMD_Output);

        temp_Int = str_CMD_Output.indexOf("JAVA_HOME"); //trace("temp_Int :" + temp_Int);
        str_Java_Home = str_CMD_Output.substring( temp_Int, (str_CMD_Output.indexOf("\n", temp_Int ) ) );

        temp_Int = str_CMD_Output.indexOf("JDK_HOME"); //trace("temp_Int :" + temp_Int);
        str_JDK_Home = str_CMD_Output.substring( temp_Int, (str_CMD_Output.indexOf("\n", temp_Int ) ) );

        temp_Int = str_CMD_Output.indexOf("JRE_HOME"); //trace("temp_Int :" + temp_Int)
        str_JRE_Home = str_CMD_Output.substring( temp_Int, (str_CMD_Output.indexOf("\n", temp_Int ) ) );

        trace("==================================");

        trace("str_Java_Home : " + str_Java_Home);
        trace("str_JDK_Home  : " + str_JDK_Home);
        trace("str_JRE_Home  : " + str_JRE_Home);

    }


    public function STD_onOutputData(event:ProgressEvent):void 
    { 
        trace("## doing FUNCTION : STD_onOutputData( ... )" );

        if (NProcess && NProcess.running)
        {
            if (NProcess.standardOutput.bytesAvailable > 0)
            {
                trace("got bytes in... STD OUT : ");

                //# Receive CMD.exe output (UTF bytes) into String...
                str_CMD_Output = NProcess.standardOutput.readUTFBytes(NProcess.standardOutput.bytesAvailable);

                trace("str_CMD_Output.length : " + str_CMD_Output.length + " characters");

                trace("## end of... STD_Out ::: ");

                update_String_with_Path();

            }

        }
    }


    public function STD_onErrorData(event:ProgressEvent):void 
    {
        trace("## doing FUNCTION : STD_onErrorData( ... )" );

        if (NProcess.running == true)
        {
            //# sometimes Native Process output is in std-Error instead of std-Output
            //# (is decided by the running .EXE file)

            trace("## end of... STD_Error ::: ");
        }
    }


    public function STD_onIOError(event:IOErrorEvent):void 
    { trace(event.toString()); }


    private function STD_errorHandler(e:AsyncErrorEvent):void 
    { trace('ERROR: ' + e.text); }


} //end Class

} //end Package

这篇关于AS3 - 获取 JAVA Home 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆