|
Ant example source code file (JavaEnvUtils.java)
The JavaEnvUtils.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.util;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.Vector;
import org.apache.tools.ant.taskdefs.condition.Os;
/**
* A set of helper methods related to locating executables or checking
* conditons of a given Java installation.
*
* @since Ant 1.5
*/
public final class JavaEnvUtils {
private JavaEnvUtils() {
}
/** Are we on a DOS-based system */
private static final boolean IS_DOS = Os.isFamily("dos");
/** Are we on Novell NetWare */
private static final boolean IS_NETWARE = Os.isName("netware");
/** Are we on AIX */
private static final boolean IS_AIX = Os.isName("aix");
/** shortcut for System.getProperty("java.home") */
private static final String JAVA_HOME = System.getProperty("java.home");
/** FileUtils instance for path normalization */
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
/** Version of currently running VM. */
private static String javaVersion;
/** floating version of the JVM */
private static int javaVersionNumber;
/** Version constant for Java 1.0 */
public static final String JAVA_1_0 = "1.0";
/** Version constant for Java 1.1 */
public static final String JAVA_1_1 = "1.1";
/** Version constant for Java 1.2 */
public static final String JAVA_1_2 = "1.2";
/** Version constant for Java 1.3 */
public static final String JAVA_1_3 = "1.3";
/** Version constant for Java 1.4 */
public static final String JAVA_1_4 = "1.4";
/** Version constant for Java 1.5 */
public static final String JAVA_1_5 = "1.5";
/** Version constant for Java 1.6 */
public static final String JAVA_1_6 = "1.6";
/** Whether this is the Kaffe VM */
private static boolean kaffeDetected;
/** array of packages in the runtime */
private static Vector jrePackages;
static {
// Determine the Java version by looking at available classes
// java.net.Proxy was introduced in JDK 1.5
// java.lang.CharSequence was introduced in JDK 1.4
// java.lang.StrictMath was introduced in JDK 1.3
// java.lang.ThreadLocal was introduced in JDK 1.2
// java.lang.Void was introduced in JDK 1.1
// Count up version until a NoClassDefFoundError ends the try
try {
javaVersion = JAVA_1_0;
javaVersionNumber = 10;
Class.forName("java.lang.Void");
javaVersion = JAVA_1_1;
javaVersionNumber++;
Class.forName("java.lang.ThreadLocal");
javaVersion = JAVA_1_2;
javaVersionNumber++;
Class.forName("java.lang.StrictMath");
javaVersion = JAVA_1_3;
javaVersionNumber++;
Class.forName("java.lang.CharSequence");
javaVersion = JAVA_1_4;
javaVersionNumber++;
Class.forName("java.net.Proxy");
javaVersion = JAVA_1_5;
javaVersionNumber++;
Class.forName("java.util.ServiceLoader");
javaVersion = JAVA_1_6;
javaVersionNumber++;
} catch (Throwable t) {
// swallow as we've hit the max class version that
// we have
}
kaffeDetected = false;
try {
Class.forName("kaffe.util.NotImplemented");
kaffeDetected = true;
} catch (Throwable t) {
// swallow as this simply doesn't seem to be Kaffe
}
}
/**
* Returns the version of Java this class is running under.
* @return the version of Java as a String, e.g. "1.1"
*/
public static String getJavaVersion() {
return javaVersion;
}
/**
* Returns the version of Java this class is running under.
* This number can be used for comparisions; it will always be
* @return the version of Java as a number 10x the major/minor,
* e.g Java1.5 has a value of 15
*/
public static int getJavaVersionNumber() {
return javaVersionNumber;
}
/**
* Compares the current Java version to the passed in String -
* assumes the argument is one of the constants defined in this
* class.
* Note that Ant now requires JDK 1.2+ so {@link #JAVA_1_0} and
* {@link #JAVA_1_1} need no longer be tested for.
* @param version the version to check against the current version.
* @return true if the version of Java is the same as the given version.
* @since Ant 1.5
*/
public static boolean isJavaVersion(String version) {
return javaVersion.equals(version);
}
/**
* Compares the current Java version to the passed in String -
* assumes the argument is one of the constants defined in this
* class.
* Note that Ant now requires JDK 1.2+ so {@link #JAVA_1_0} and
* {@link #JAVA_1_1} need no longer be tested for.
* @param version the version to check against the current version.
* @return true if the version of Java is the same or higher than the
* given version.
* @since Ant 1.7
*/
public static boolean isAtLeastJavaVersion(String version) {
return javaVersion.compareTo(version) >= 0;
}
/**
* Checks whether the current Java VM is Kaffe.
* @return true if the current Java VM is Kaffe.
* @since Ant 1.6.3
* @see <a href="http://www.kaffe.org/">http://www.kaffe.org/
*/
public static boolean isKaffe() {
return kaffeDetected;
}
/**
* Finds an executable that is part of a JRE installation based on
* the java.home system property.
*
* <p>
Other Ant examples (source code examples)Here is a short list of links related to this Ant JavaEnvUtils.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.