1#!/usr/bin/env python3 2 3# CDDL HEADER START 4# 5# The contents of this file are subject to the terms of the 6# Common Development and Distribution License (the "License"). 7# You may not use this file except in compliance with the License. 8# 9# See LICENSE.txt included in this distribution for the specific 10# language governing permissions and limitations under the License. 11# 12# When distributing Covered Code, include this CDDL HEADER in each 13# file and include the License file at LICENSE.txt. 14# If applicable, add the following below this CDDL HEADER, with the 15# fields enclosed by brackets "[]" replaced with your own identifying 16# information: Portions Copyright [yyyy] [name of copyright owner] 17# 18# CDDL HEADER END 19 20# 21# Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. 22# Portions Copyright (c) 2017-2018, Chris Fraire <cfraire@me.com>. 23# 24 25import os 26import platform 27import shutil 28 29from .command import Command 30from .utils import is_exe 31 32 33class Java(Command): 34 """ 35 java executable wrapper class 36 """ 37 38 def __init__(self, command, logger=None, main_class=None, java=None, 39 jar=None, java_opts=None, classpath=None, env_vars=None, 40 redirect_stderr=True, doprint=False): 41 42 if not java: 43 java = self.FindJava(logger) 44 if not java: 45 raise Exception("Cannot find Java") 46 47 if not is_exe(java): 48 raise Exception("{} is not executable file".format(java)) 49 50 logger.debug("Java = {}".format(java)) 51 52 java_command = [java] 53 if java_opts: 54 java_command.extend(java_opts) 55 if classpath: 56 java_command.append('-classpath') 57 java_command.append(classpath) 58 if jar: 59 java_command.append('-jar') 60 java_command.append(jar) 61 if main_class: 62 java_command.append(main_class) 63 env = None 64 if env_vars: 65 env = {} 66 for spec in env_vars: 67 if spec.find('=') != -1: 68 name, value = spec.split('=') 69 env[name] = value 70 71 java_command.extend(command) 72 logger.debug("Java command: {}".format(java_command)) 73 74 super().__init__(java_command, logger=logger, env_vars=env, 75 redirect_stderr=redirect_stderr, doprint=doprint) 76 77 def FindJava(self, logger): 78 """ 79 Determine Java binary based on platform. 80 """ 81 java = None 82 system_name = platform.system() 83 if system_name == 'SunOS': 84 rel = platform.release() 85 if rel == '5.10': 86 java_home = "/usr/jdk/instances/jdk1.7.0" 87 elif rel == '5.11': 88 java_home = "/usr/jdk/latest" 89 90 if os.path.isdir(java_home): 91 java = os.path.join(java_home, 'bin', 'java') 92 elif system_name == 'Darwin': 93 cmd = Command(['/usr/libexec/java_home']) 94 cmd.execute() 95 java = os.path.join(cmd.getoutputstr(), 'bin', 'java') 96 elif system_name == 'Linux': 97 link_path = '/etc/alternatives/java' 98 if os.path.exists(link_path): 99 # Resolve the symlink. 100 java = os.path.realpath(link_path) 101 102 if not java: 103 java_home = os.environ.get('JAVA_HOME') 104 if java_home: 105 logger.debug("Could not detemine Java home using standard " 106 "means, trying JAVA_HOME: {}".format(java_home)) 107 if os.path.isdir(java_home): 108 java = os.path.join(java_home, 'bin', 'java') 109 110 if not java: 111 logger.debug("Could not detemine java executable using standard " 112 "means, trying system path") 113 java = shutil.which('java') 114 115 return java 116