Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

actionscript 3 - AS3 - Get JAVA Home path

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Could you help to fix it with Environment Variables?

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.

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

You can use terminal commands to find JAVA_HOME.

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

/usr/libexec/java_home


Windows OS: (using cmd.exe command line)

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 FilesJavajre1.8.0_102
JDK_HOME=C:Program FilesJavajdk1.8.0_102
JRE_HOME=C:Program FilesJavajre1.8.0_102jre

Option 2 : (gets system's current Java.exe path)

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

which gives me one result of : C:Program FilesJavajre1.8.0_102injava.exe

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) Check for Java current version used by Windows...

REG QUERY HKEY_LOCAL_MACHINESOFTWAREJavaSoft /v "CurrentVersion" /s

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

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

REG QUERY HKEY_LOCAL_MACHINESOFTWAREJavaSoft /v "JavaHome" /s


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

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

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).


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

(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").

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("
", 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("
", 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("
", 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...