You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by se...@apache.org on 2013/11/17 01:48:01 UTC

svn commit: r1542622 - /jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java

Author: sebb
Date: Sun Nov 17 00:48:01 2013
New Revision: 1542622

URL: http://svn.apache.org/r1542622
Log:
Add a method to find the Java home directory

Modified:
    jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java

Modified: jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java?rev=1542622&r1=1542621&r2=1542622&view=diff
==============================================================================
--- jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java (original)
+++ jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java Sun Nov 17 00:48:01 2013
@@ -19,11 +19,13 @@
 package org.apache.jorphan.util;
 
 import java.io.Closeable;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -567,4 +569,25 @@ public final class JOrphanUtils {
             offset += chunk;
         }
     }
+
+    /**
+     * Find where the running instance of Java is installed (if possible).
+     * May not work with all versions of Java.
+     * 
+     * @return the home location of Java, or {@code null} if the method fails
+     */
+	public static File getJavaHome() {
+		// For example: jar:file:/C:/jdk1.6.0_45/jre/lib/rt.jar!/java/lang/Object.class
+		URL resource = Object.class.getResource("Object.class"); // might not work with some Java implementations
+		if (resource == null) {
+			return null;
+		}
+		String path = resource.getPath();
+		// For example: file:/C:/jdk1.6.0_45/jre/lib/rt.jar!/java/lang/Object.class
+		path = path.replace("file:","").replaceFirst("!.+", "");
+		// e.g. /C:/jdk1.6.0_45/jre/lib/rt.jar
+		File rt = new File(path);
+		return rt.getParentFile().getParentFile().getParentFile();
+	}
+    
 }
\ No newline at end of file