|
| 1 | +import java.io.File; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStream; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.io.PrintWriter; |
| 6 | +import java.io.StringWriter; |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Provide simple discovery of java compilation options similar |
| 12 | + * to package *-config programs installed with a lot of native software. |
| 13 | + * <p> |
| 14 | + * Most of this is derived from the system properties. An example is here: |
| 15 | + * <pre> |
| 16 | + java.runtime.name=OpenJDK Runtime Environment |
| 17 | + sun.boot.library.path=/usr/lib/jvm/java-6-openjdk/jre/lib/amd64 |
| 18 | + java.vm.version=19.0-b09 |
| 19 | + java.vm.vendor=Sun Microsystems Inc. |
| 20 | + java.vendor.url=http\://java.sun.com/ |
| 21 | + path.separator=\: |
| 22 | + java.vm.name=OpenJDK 64-Bit Server VM |
| 23 | + file.encoding.pkg=sun.io |
| 24 | + sun.java.launcher=SUN_STANDARD |
| 25 | + user.country=US |
| 26 | + sun.os.patch.level=unknown |
| 27 | + java.vm.specification.name=Java Virtual Machine Specification |
| 28 | + user.dir=/home/stella/branches/java-config |
| 29 | + java.runtime.version=1.6.0_20-b20 |
| 30 | + java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment |
| 31 | + java.endorsed.dirs=/usr/lib/jvm/java-6-openjdk/jre/lib/endorsed |
| 32 | + os.arch=amd64 |
| 33 | + java.io.tmpdir=/tmp |
| 34 | + line.separator=\n |
| 35 | + java.vm.specification.vendor=Sun Microsystems Inc. |
| 36 | + os.name=Linux |
| 37 | + sun.jnu.encoding=UTF-8 |
| 38 | + java.library.path=/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server\:/usr/lib/jvm/java-6-openjdk/jre/lib/amd64\:/usr/lib/jvm/java-6-openjdk/jre/../lib/amd64\:/usr/lib64/xulrunner-addons\:/usr/java/packages/lib/amd64\:/usr/lib/jni\:/lib\:/usr/lib |
| 39 | + java.specification.name=Java Platform API Specification |
| 40 | + java.class.version=50.0 |
| 41 | + sun.management.compiler=HotSpot 64-Bit Server Compiler |
| 42 | + os.version=2.6.35-25-generic |
| 43 | + user.home=/home/stella |
| 44 | + user.timezone=America/Los_Angeles |
| 45 | + java.awt.printerjob=sun.print.PSPrinterJob |
| 46 | + file.encoding=UTF-8 |
| 47 | + java.specification.version=1.6 |
| 48 | + java.class.path=/home/stella/branches/java-config/bin |
| 49 | + user.name=stella |
| 50 | + java.vm.specification.version=1.0 |
| 51 | + java.home=/usr/lib/jvm/java-6-openjdk/jre |
| 52 | + sun.arch.data.model=64 |
| 53 | + user.language=en |
| 54 | + java.specification.vendor=Sun Microsystems Inc. |
| 55 | + java.vm.info=mixed mode |
| 56 | + java.version=1.6.0_20 |
| 57 | + java.ext.dirs=/usr/lib/jvm/java-6-openjdk/jre/lib/ext\:/usr/java/packages/lib/ext |
| 58 | + sun.boot.class.path=/usr/lib/jvm/java-6-openjdk/jre/lib/resources.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/sunrsasign.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/jsse.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/jce.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/netx.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/plugin.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/rhino.jar\:/usr/lib/jvm/java-6-openjdk/jre/lib/modules/jdk.boot.jar\:/usr/lib/jvm/java-6-openjdk/jre/classes |
| 59 | + java.vendor=Sun Microsystems Inc. |
| 60 | + file.separator=/ |
| 61 | + java.vendor.url.bug=http\://java.sun.com/cgi-bin/bugreport.cgi |
| 62 | + sun.io.unicode.encoding=UnicodeLittle |
| 63 | + sun.cpu.endian=little |
| 64 | + sun.desktop=gnome |
| 65 | + sun.cpu.isalist= |
| 66 | + * </pre> |
| 67 | + * @author stella |
| 68 | + * |
| 69 | + */ |
| 70 | +public class JavaConfig { |
| 71 | + private String[] args; |
| 72 | + private StringWriter buffer; |
| 73 | + private PrintWriter out; |
| 74 | + |
| 75 | + public JavaConfig(String[] args) { |
| 76 | + this.args=args; |
| 77 | + this.buffer=new StringWriter(); |
| 78 | + this.out=new PrintWriter(buffer); |
| 79 | + } |
| 80 | + |
| 81 | + private String readResource(String name) throws IOException { |
| 82 | + InputStream in=getClass().getResourceAsStream(name); |
| 83 | + if (in==null) return null; |
| 84 | + |
| 85 | + StringBuilder buffer=new StringBuilder(); |
| 86 | + InputStreamReader reader=new InputStreamReader(in, "UTF-8"); |
| 87 | + char[] b=new char[1024]; |
| 88 | + for (;;) { |
| 89 | + int r=reader.read(b); |
| 90 | + if (r<0) break; |
| 91 | + buffer.append(b, 0, r); |
| 92 | + } |
| 93 | + |
| 94 | + return buffer.toString(); |
| 95 | + } |
| 96 | + |
| 97 | + public void usage() throws IOException { |
| 98 | + String buildNumber=readResource("build.txt"); |
| 99 | + if (buildNumber!=null) { |
| 100 | + System.err.println("java-config version " + buildNumber); |
| 101 | + } else { |
| 102 | + System.err.println("java-config unknown version"); |
| 103 | + } |
| 104 | + System.err.print(readResource("usage.txt")); |
| 105 | + System.exit(2); |
| 106 | + } |
| 107 | + |
| 108 | + public void error(String msg) { |
| 109 | + System.err.println(msg); |
| 110 | + System.exit(2); |
| 111 | + } |
| 112 | + |
| 113 | + public void reportProp(String prop) { |
| 114 | + String value=System.getProperty(prop); |
| 115 | + if (value==null) { |
| 116 | + error("Expected system property " + prop + " but it was not found"); |
| 117 | + } |
| 118 | + |
| 119 | + out.println(value); |
| 120 | + } |
| 121 | + |
| 122 | + private File getJdkHome() { |
| 123 | + String javaHome=System.getProperty("java.home"); |
| 124 | + if (javaHome==null) return null; |
| 125 | + File jreDir=new File(javaHome); |
| 126 | + |
| 127 | + // Probe for jre within jdk |
| 128 | + if (!new File(jreDir, "include").isDirectory()) { |
| 129 | + if (jreDir.getName().equals("jre")) { |
| 130 | + return jreDir.getParentFile(); |
| 131 | + } |
| 132 | + } else { |
| 133 | + return jreDir; |
| 134 | + } |
| 135 | + |
| 136 | + return null; |
| 137 | + } |
| 138 | + public void reportJdkHome() { |
| 139 | + File jdkDir=getJdkHome(); |
| 140 | + if (jdkDir==null) error("Could not determine JDK location"); |
| 141 | + out.println(jdkDir.getAbsolutePath()); |
| 142 | + } |
| 143 | + |
| 144 | + public void reportCFlags() { |
| 145 | + File jdkDir=getJdkHome(); |
| 146 | + if (jdkDir==null) error("Could not determine JDK location"); |
| 147 | + |
| 148 | + StringBuilder flags=new StringBuilder(); |
| 149 | + |
| 150 | + File incDir=new File(jdkDir, "include"); |
| 151 | + if (!new File(incDir, "jni.h").exists()) error("Could not find jni.h in JDK include directory " + incDir); |
| 152 | + flags.append("-I").append(incDir.getAbsolutePath()); |
| 153 | + |
| 154 | + // Scan for the platform specific directory. This is stupid. |
| 155 | + for (File child: incDir.listFiles()) { |
| 156 | + if (child.isDirectory() && new File(child, "jni_md.h").exists()) { |
| 157 | + flags.append(" -I").append(child.getAbsolutePath()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + out.println(flags.toString()); |
| 162 | + } |
| 163 | + |
| 164 | + public void run() throws IOException { |
| 165 | + if (args.length==0) { |
| 166 | + usage(); |
| 167 | + System.exit(1); |
| 168 | + } |
| 169 | + for (int i=0; i<args.length; i++) { |
| 170 | + String flag=args[i]; |
| 171 | + flag=flag.replaceFirst("^\\-+", "").toLowerCase(); |
| 172 | + |
| 173 | + if (flag.equals("home")) reportProp("java.home"); |
| 174 | + else if (flag.equals("jdkhome")) reportJdkHome(); |
| 175 | + else if (flag.equals("arch")) reportProp("os.arch"); |
| 176 | + else if (flag.equals("version")) reportProp("java.version"); |
| 177 | + else if (flag.equals("vmversion")) reportProp("java.vm.version"); |
| 178 | + else if (flag.equals("specversion")) reportProp("java.specification.version"); |
| 179 | + else if (flag.equals("runtimeversion")) reportProp("java.runtime.version"); |
| 180 | + else if (flag.equals("osversion")) reportProp("os.version"); |
| 181 | + else if (flag.equals("osname")) reportProp("os.name"); |
| 182 | + else if (flag.equals("datamodel")) reportProp("sun.arch.data.model"); |
| 183 | + else if (flag.equals("endian")) reportProp("sun.cpu.endian"); |
| 184 | + else if (flag.equals("language")) reportProp("user.language"); |
| 185 | + else if (flag.equals("cflags")) reportCFlags(); |
| 186 | + } |
| 187 | + |
| 188 | + out.flush(); |
| 189 | + System.out.print(buffer.toString()); |
| 190 | + } |
| 191 | + |
| 192 | + public static void main(String[] args) throws Exception { |
| 193 | + JavaConfig m=new JavaConfig(args); |
| 194 | + m.run(); |
| 195 | + } |
| 196 | +} |
0 commit comments