Search code examples
androidtestingui-automationappium

Error: Could not get the Java version. Is Java installed?


I run Appium server:

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

I try to run this code to post to Appium server:

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);
    }

but as you can see I get this error:

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

how can I fix this?

I'm trying to automatically open an app which i downloaded from the google play and I don't have its apk.


Solution

  • I think this is the final solution for your problem.

    As always, make a backup copy of the file before editing.

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

    In the else if(stderr) branch around line 1034:

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

    replace the code to read:

    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,'');
        }
    }
    

    The code should work with a regular java version response message plus the one that you are receiving.