Is -Djava.Library.Path=... Equivalent to System.Setproperty("Java.Library.Path", ...)

Why can't System.setProperty() change the classpath at runtime?

You can certainly set any system properties you want at any point of time. The question is, will it have any effect? In the case of classpath, the answer is NO. The system class loader is initialized at a very early point in the startup sequence. It copies the classpath into its own data structures, and the classpath property is not read again. Changing it affect nothing in the system.

The reason for this may be two-fold. The lesser reason is performance. You may need to have some sort of data structure built for quick lookup of resources, and re-parsing classpath every time may be inefficient. The more important reason is security. You don't want a rogue class change the classpath under you and load compromised version of another class.

Java: System.getProperty(user.home) returns ?

It's a bit embarrassing but the solution was simply to use a 64-bit JDK on a 64-bit system. I copied everything from my old machine, which meant also a 32-bit JDK, and this was the problem. It worked as expected with a 64-bit runtime.

Sorry for bothering.

Difference between java.home and JAVA_HOME

As you stated, JAVA_HOME points to the JDK installation path given by the Environment Variable(%JAVA_HOME%).

But java.home points to the JRE installation path, now it returns the JRE that was used to run the application, please remember that you can have multiple versions of JRE and JDK on the same server/computer

And you can run an application specifying what jre or jdk you want to use.

So, for example, if you have on your Environment path:

%JAVA_HOME% = C:\Program Files\Java\jdk1.6.0_24

But if you ran the application using an specific jre:

"C:\Program Files (x86)\Java\jre1.8.0_73\bin\java" -jar TheJavaFile.jar

Inside the application on run-time, you will get on java.home a different version of the JAVA_HOME

This may explain why on some cases you get different versions for both variable and system property.

Also, please notice that the paths may be quite different, since JRE is a different product than JDK, then they are installed in different locations, because they are independent

Now, regarding what's the difference from one JDK vs JRE, this diagram explains it pretty clear:

Sample Image

JDK is a superset of JRE, and contains everything that is in JRE, plus
tools such as the compilers and debuggers necessary for developing
applets and applications. JRE provides the libraries, the Java Virtual
Machine (JVM), and other components to run applets and applications
written in the Java programming language.

System.getProperty(java.class.path) does not show WEB-INF/lib and the including jars

They way you are setting the java.class.path system property is asking for trouble - better not do that. More elegant approach would be to use the -classpath option to pass in the custom classpath. See How do I use JDK6 ToolProvider and JavaCompiler with the context classloader? for details.

Also this question can be useful reference: Using javax.tools.ToolProvider from a custom classloader?


As to building the actual classpath, you could cast the context classloader to URLClassLoader and get files from those URLs (as done in this answer).

Or you could use ServletContext.getRealPath(String) and build the entire classpath by hand:

ServletConfig cfg = ...; //obtained in Servlet.init(ServletConfig) method
ServletContex ctx = cfg.getServletContext();
String realWebInfPath = ctx.getRealPath("WEB-INF/lib");
//TODO use the realWebInfPath to create a File object and iterate over all JAR files

Warning: both approaches ONLY work if web application is expanded (not WAR file). If it is not expanded, you are out of luck.

What is the cross-platform way of obtaining the path to the local application data directory?

You could probably say something like (contradict me if I am wrong, or if this a bad approach)

private String workingDirectory;
//here, we assign the name of the OS, according to Java, to a variable...
private String OS = (System.getProperty("os.name")).toUpperCase();
//to determine what the workingDirectory is.
//if it is some version of Windows
if (OS.contains("WIN"))
{
//it is simply the location of the "AppData" folder
workingDirectory = System.getenv("AppData");
}
//Otherwise, we assume Linux or Mac
else
{
//in either case, we would start in the user's home directory
workingDirectory = System.getProperty("user.home");
//if we are on a Mac, we are not done, we look for "Application Support"
workingDirectory += "/Library/Application Support";
}
//we are now free to set the workingDirectory to the subdirectory that is our
//folder.

Note that, in this code, I am taking full advantage that Java treats '/' the same as '\\' when dealing with directories. Windows uses '\\' as pathSeparator, but it is happy with '/', too. (At least Windows 7 is.) It is also case-insensitive on it's environment variables; we could have just as easily said workingDirectory = System.getenv("APPDATA"); and it would have worked just as well.

What is the best way to find the user's home directory in Java?

The bug you reference (bug 4787391) has been fixed in Java 8. Even if you are using an older version of Java, the System.getProperty("user.home") approach is probably still the best. The user.home approach seems to work in a very large number of cases. A 100% bulletproof solution on Windows is hard, because Windows has a shifting concept of what the home directory means.


If user.home isn't good enough for you I would suggest choosing a definition of home directory for windows and using it, getting the appropriate environment variable with System.getenv(String).

how to set system properties in C#

try System.Environment.SetEnvironmentVariable("webdriver.chrome.driver",@"/path/to/where/you/ve/put/chromedriver.exe")
-MSDN



Related Topics



Leave a reply



Submit