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

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

问题描述

我需要在Java Home lib目录下复制一个扩展名。
但是我的问题是在Windows和Mac OS上找到java家。
这个目录在每个idk更新上都会改变。
你可以帮助解决它的环境变量。
最好的问候。

解决方案


你可以用环境变量


你能确定你的应用程序用户 strong>在其帐户的环境设置中添加了 JAVA_HOME 路径?如果他们没有遵循一些教程,那么你的想法就什么也找不到。


但是我的问题是找到 JAVA_HOME您可以使用终端命令来查找 在Windows和Mac OS上。

JAVA_HOME



Mac OS :(

  / usr / libexec  / java_home 



Windows操作系统 cmd.exe 命令行)

选项1 :通常 set 将新条目添加到环境变量中,但是如果没有参数使用,它将只列出现有的环境变量。
$ b

  set 

我的结果长文本包括:

$ $ p $ code JAVA_HOME = C:\程序文件\Java\jre1.8.0_102
JDK_HOME = C:\程序文件\Java\jdk1.8.0_102
JRE_HOME = C:\程序文件\Java\jre1.8.0 _102 \ jre

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

  for%i in (java.exe)做@echo。 %〜$ PATH:

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



选项3 :对于Windows,我认为最好的选择是检查用户的 currentVersion 的Windows 注册表,然后检查< (a)检查当前使用的Java 当前版本。 Windows ...

  REG QUERY HKEY_LOCAL_MACHINE \SOFTWARE\JavaSoft\ / vCurrentVersion/ s 

注意: (b)检查Java 主路径 1.8
1.8.0_102 )...

  REG QUERY HKEY_LOCAL_MACHINE \ SOFTWARE \\ javaSoft \ /JavaHome/ s 




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



<稍后,在AS3代码中,可以使用正则表达式字符串函数提取数字或目录路径您需要。

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






您询问了有关环境变量(不是注册表)的内容,下面是一个快速的例子,用于Windows的 Option 1


如果在IDE中进行测试时只勾选 Extended Desktop(扩展桌面),则

在您的AIR设置中,稍后在导出的运行时版本中,您必须取消勾选,现在只勾选桌面。)

Windows默认打开CMD的示例代码.exe并传递 set 命令,然后接收其返回的文本输出。您可以缩短代码...

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $进口flash.display.MovieClip ;

导入flash.events。*;
导入flash.errors。*;

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

导入flash.filesystem.File;

import flash.utils.ByteArray;


public class Java_Get_Path_v3扩展MovieClip
{

private var NProcessExe:File;

private var NProcess:NativeProcess; //外部进程(例如:notepad.exe)
private var nativeProcessStartupInfo:NativeProcessStartupInfo;
private var NProcess_Args:Vector。< String> = new Vector。< String>(); //进程启动选项

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;

$ b public function Java_Get_Path_v3()
{
//#1)setup本机进程(命令行在后台运行(无窗口))
createNativeProcess ();

//#2)通过Environment Variables从Windows获取JavaHome路径;
get_Java_Path(); //是通过stdout



$ b private函数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();

//#添加事件的进程监听器
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(找不到指定的.exe文件); }

}
else //#if NOT suppoNativeProcess。
{
trace(本机进程在此模式下不受支持(调试时只使用\Extended Desktop \));



$ b私有函数get_Java_Path():void
{
//#用于获取Java Home路径
NProcess_Args [0] =(\ / c+set);

##用于获得Java版本
// NProcess_Args [0] =(\ / c+java -version);

//#启动本机进程(例如:静音运行CMD.exe)
//#triggers :::: public function STD_onOutputData $ b $ nativeProcessStartupInfo = new NativeProcessStartupInfo ;
nativeProcessStartupInfo.executable = NProcessExe;
nativeProcessStartupInfo.arguments = NProcess_Args;

NProcess.start(nativeProcessStartupInfo);



$ b private function update_String_with_Path():void
{
trace(## doing FUNCTION:update_String_with_Path()) ;
$ b $ // 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(\ 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);



$ b public function STD_onOutputData(event:ProgressEvent):void
{
trace(## doing FUNCTION:STD_onOutputData( ...)); (NProcess& NProcess.running)

if(NProcess.standardOutput.bytesAvailable> 0)
{
trace( 得到字节在... STD OUT:);

//将CMD.exe输出(UTF字节)接收到字符串中...
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();





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

if(NProcess.running == true)
{
//#有时候本机进程输出在std-Error而不是std-Output
//# (由运行的.EXE文件决定)

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



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


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

$ b} // end Class
$ b} // end Package


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.

解决方案

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

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 Files\Java\jre1.8.0_102\bin\java.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_MACHINE\SOFTWARE\JavaSoft\ /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_MACHINE\SOFTWARE\JavaSoft\ /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("\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天全站免登陆