Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Libcore.os设置环境变量_Java_Android_Android Ndk - Fatal编程技术网

Java 如何使用Libcore.os设置环境变量

Java 如何使用Libcore.os设置环境变量,java,android,android-ndk,Java,Android,Android Ndk,我试图使用Libcore设置环境变量,因此我使用了以下答案: 在Android上,该界面通过Libcore.os作为一种隐藏API公开 Libcore.os.setenv(“VAR”,“value”,bOverwrite); Libcore.os.getenv(“VAR”) Libcore类以及接口操作系统都是公共的。只是缺少类声明,需要向链接器显示。不需要将类添加到应用程序中,但如果包含这些类,也不会有任何影响 package libcore.io; public final class

我试图使用Libcore设置环境变量,因此我使用了以下答案:

在Android上,该界面通过Libcore.os作为一种隐藏API公开

Libcore.os.setenv(“VAR”,“value”,bOverwrite); Libcore.os.getenv(“VAR”)

Libcore类以及接口操作系统都是公共的。只是缺少类声明,需要向链接器显示。不需要将类添加到应用程序中,但如果包含这些类,也不会有任何影响

package libcore.io;

public final class Libcore {
    private Libcore() { }

    public static Os os;
}

package libcore.io;

public interface Os {
    public String getenv(String name);
    public void setenv(String name, String value, boolean overwrite) throws Exception;
}
然而,当我这样做时,它似乎不像在我的C代码中执行getenv时那样工作,我没有得到任何返回值

当我这么做的时候

char* p;
setenv("VAR", "test variable", 1);
p = getenv("VAR");
LOG(ERROR) << "Test" << p;
我的C代码如下:

char* p;
p = getenv("VAR");
LOG(ERROR) << "Test" << p;
char*p;
p=getenv(“VAR”);

LOG(ERROR)您可以使用反射动态调用该类

try {
     Class < ? > libcore = Class.forName("libcore.io.Libcore");
     if (libcore == null) {
      Log.w(TAG, "Cannot find libcore.os.Libcore;");
      //                return;
     } else {
      final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);

      Method method = os.getClass().getMethod("setenv", String.class, String.class, boolean.class);
      method.invoke(os, "value", "test", true);



     }
    } catch (Exception e) {
     Log.w("Sam1", Log.getStackTraceString(e));
     Log.w("Sam1", e.getMessage());
    }
试试看{
Class<?>libcore=Class.forName(“libcore.io.libcore”);
if(libcore==null){
Log.w(标记“找不到libcore.os.libcore;”);
//返回;
}否则{
最终对象os=Class.forName(“libcore.io.libcore”).getField(“os”).get(null);
方法Method=os.getClass().getMethod(“setenv”,String.class,String.class,boolean.class);
调用(os,“值”,“测试”,真);
}
}捕获(例外e){
Log.w(“Sam1”,Log.getStackTraceString(e));
w(“Sam1”,e.getMessage());
}

您可以使用反射动态调用该类

try {
     Class < ? > libcore = Class.forName("libcore.io.Libcore");
     if (libcore == null) {
      Log.w(TAG, "Cannot find libcore.os.Libcore;");
      //                return;
     } else {
      final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);

      Method method = os.getClass().getMethod("setenv", String.class, String.class, boolean.class);
      method.invoke(os, "value", "test", true);



     }
    } catch (Exception e) {
     Log.w("Sam1", Log.getStackTraceString(e));
     Log.w("Sam1", e.getMessage());
    }
试试看{
Class<?>libcore=Class.forName(“libcore.io.libcore”);
if(libcore==null){
Log.w(标记“找不到libcore.os.libcore;”);
//返回;
}否则{
最终对象os=Class.forName(“libcore.io.libcore”).getField(“os”).get(null);
方法Method=os.getClass().getMethod(“setenv”,String.class,String.class,boolean.class);
调用(os,“值”,“测试”,真);
}
}捕获(例外e){
Log.w(“Sam1”,Log.getStackTraceString(e));
w(“Sam1”,e.getMessage());
}