错误:无法获取Java版本。是Java安装?

时间:2015-02-25 13:28:38

标签: android testing ui-automation appium

我运行Appium服务器:

➜  ~  appium    
info: Welcome to Appium v1.3.5 (REV a124a15677e26b33db16e81c4b3b34d9c6b8cac9)
info: Appium REST http interface listener started on 0.0.0.0:4723
info: Console LogLevel: debug
info: --> POST /wd/hub/session {"desiredCapabilities":{"appPackage":"com.grindrapp.android","appActivity":".activity.SplashActivity","platformVersion":"4.4.2","browserName":"","platformName":"Android","deviceName":"10.0.0.9:5555"}}
info: Client User-Agent string: Apache-HttpClient/4.3.4 (java 1.5)
info: [debug] Didn't get app but did get Android package, will attempt to launch it on the device
info: [debug] Creating new appium session 090abe0c-36d9-4f9b-987a-f3b665045928
info: Starting android appium
info: [debug] Getting Java version
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
error: Failed to start an Appium session, err was: Error: Could not get the Java version. Is Java installed?
info: [debug] Error: Could not get the Java version. Is Java installed?
    at /usr/local/lib/node_modules/appium/lib/devices/android/android-common.js:1040:17
    at ChildProcess.exithandler (child_process.js:735:7)
    at ChildProcess.emit (events.js:110:17)
    at maybeClose (child_process.js:1008:16)
    at Socket.<anonymous> (child_process.js:1176:11)
    at Socket.emit (events.js:107:17)
    at Pipe.close (net.js:476:12)
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Could not get the Java version. Is Java installed?)","origValue":"Could not get the Java version. Is Java installed?"},"sessionId":null}
info: <-- POST /wd/hub/session 500 316.738 ms - 222 


^C
➜  ~  echo $JAVA_HOME 
/Library/Java/JavaVirtualMachines/jdk1.7.0_76.jdk/Contents/Home

我尝试运行此代码以发布到Appium服务器:

public class AndroidTest {

    private AppiumDriver driver;

    @Before
    public void setUp() throws Exception {
//        File classpathRoot = new File(System.getProperty("user.dir"));
//        File appDir = new File(classpathRoot, "../../../data/app/");
//        File app = new File(appDir, "Facebook.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("deviceName","10.0.0.9:5555");
        capabilities.setCapability("platformVersion", "4.4.2");
        capabilities.setCapability("platformName","Android");
        //capabilities.setCapability("app", app.getCanonicalPath());

        capabilities.setCapability("appPackage", "com.grindrapp.android");
        capabilities.setCapability("appActivity", ".activity.SplashActivity");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

但是你可以看到我收到这个错误:

info: [debug] Error: Could not get the Java version. Is Java installed?

我该如何解决这个问题?

我正在尝试自动打开我从谷歌播放下载的应用程序,但我没有它的apk。

3 个答案:

答案 0 :(得分:1)

我认为这是您问题的最终解决方案。

与往常一样,在编辑之前制作文件的备份副本。

编辑文件/usr/local/lib/node_modules/appium/lib/devices/android/android-common.js

在第1034行的else if(stderr)分支中:

else if (stderr) {
        var firstLine = stderr.split("\n")[0];
        if (new RegExp("java version").test(firstLine)) {
          javaVersion = firstLine.split(" ")[2].replace(/"/g, '');
        }
      }

将代码替换为:

else if (stderr){
    if(new RegExp("java version").test(stderr)){
      var regex = "java version \"[0-9]+\.[0-9]+\.[0-9]+";
      var stderrstring = '' + stderr + '';
      var regmatch = stderrstring.match(regex);
      javaVersion = '' + regmatch + ''   //convert object to string
      javaVersion.replace(/"/g,'');
    }
}

代码应该使用常规的Java版本响应消息以及您正在接收的消息。

答案 1 :(得分:0)

该文件位于:

  

/usr/local/lib/node_modules/appium/lib/devices/android/android-common.js

我们关注从第1029行开始的代码是:

androidCommon.getJavaVersion = function (cb) {
    exec("java -version", function (err, stdout, stderr) {
      var javaVersion = null;
      if (err) {
        return cb(new Error("'java -version' failed. " + err));
      } else if (stderr) {
        var firstLine = stderr.split("\n")[0];
        if (new RegExp("java version").test(firstLine)) {
          javaVersion = firstLine.split(" ")[2].replace(/"/g, '');
        }
      }
      if (javaVersion === null) {
        return cb(new Error("Could not get the Java version. Is Java installed?"));
      }
      return cb(null, javaVersion);
    });
};

我无法在我的系统上测试这个,但我认为成功执行java -version会在stdout而不是stderr上返回它的响应。我会更改代码以提供有关正在发生的事情的更多信息并帮助调试它。

你在列表的末尾得到了JavaVersion = null消息,这意味着在err中没有返回任何内容,并且stderr的解析没有设置JavaVersion。它没有告诉你stderr中是否有任何东西,甚至没有看到stdout。

这是修订后的代码清单。如果确实在stderr上返回了输出,则代码现在至少会将其打印出来,以便您可以看到返回的内容。我为stdout添加了相同的代码分支,因为它根本没有检查过。

androidCommon.getJavaVersion = function (cb) {
exec("java -version", function (err, stdout, stderr) {
    var javaVersion = null;
    var firstLine = null;

    if (err) {
      return cb(new Error("'java -version' failed. " + err));
    } else if (stderr) {
        firstLine = stderr.split("\n")[0];

        if (new RegExp("java version").test(firstLine)) {
          javaVersion = firstLine.split(" ")[2].replace(/"/g, '');
        }else{
          return cb(new Error("Java version stderr string parse error: " + stderr));
        }
    } else if(stdout){
            firstLine = stdout.split("\n")[0];

        if (new RegExp("java version").test(firstLine)) {
          javaVersion = firstLine.split(" ")[2].replace(/"/g, '');
        }else{
          return cb(new Error("Java version stdout string parse error: " + stdout));
        }       
    }   

    if (javaVersion === null) {
      return cb(new Error("Could not get the Java version. Is Java installed?"));
    }
      return cb(null, javaVersion);
});
}

所以:

  1. 制作android-commons.js的备份副本
  2. 让代码更改并试一试。
  3. 这可能与写入有关,或者错误消息会告诉您返回的内容。

答案 2 :(得分:0)

android-common.js中的代码正在寻找第一行的java版本号。 我有类似的问题,但我的java -version返回结果为

[~]$ java -version
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
java version "1.7.0_76"
Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)

所以我只是将android-common.js中的代码更改为拆分第二行而不是第一行

firstLine = stderr.split("\n")[0]; -> firstLine = stderr.split("\n")[1]