I am using PHP Java Bridgeon a windows and linux server. Now I want to modify the
CLASSPATH. When I do a phpinfo(), I see a CLASSPATH under environment variables section and
when I output the java.lang.system properties there is a property called java.class.path.
First of all what is the difference between the two? How do I modify both of them? Can they be modified programatically ?
I don't know about the phpinfo() information, but the System.property can't be set (to any effect) directly. It is effectively read-only and taken from the environment CLASSPATH variable and -classpath command line option when Java is launched.
You can effectively change the classpath used to load classes by creating your own classloaders or class loader instances (see URLClassLoader) and using it to load from dynamic locations.
Related
Hey i wondered what is the difference between .properties file to environment variable in java.
.Properties file
arg1=text1
arg2=text2
environment variable
java -jar -arg1=text1 -arg2=text2 myJAR.jar
Passing arguments in this way is not overridable at runtime :
java -jar -arg1=text1 -arg2=text2 myJAR.jar
as the values are passed when the application is started.
You have to restart the application to change the values.
Besides, passing a lot of arguments may be error prone.
Valuing properties in this way may be overridable at runtime :
.Properties file
arg1=text1
arg2=text2
As the values are located in a external file that may be modified during the runtime of the application. Of course a way foreseen to take into consideration the file modification during the runtime has to be implemented in the application.
For example, Logback (with Joran implementation) provides a way
to change the Logback configuration file at runtime :
https://logback.qos.ch/manual/configuration.html#joranDirectly
Environment variables are key-value pairs passed to a program while it's invoked whereas properties file consists of list of properties along with their values. Generally, properties file is considered to be the best practice to maintain configuration values, here's why:
Properties file are more readable than environment variables. Also, they are not tied to startup script, we can pass any file while running the program.
Frameworks like Spring provide different ways to create a collection or map from properties file.
It's easy to use different properties files for different environments (e.g. stage, prod etc)
I'm not convinced myself why we need to set path for java to run it's applications only in environment variables, why can't in another place? please clarify my doubts.What's meant by environment variables mainly?
Actually CLASSPATH is only a convention. You can load any class only knowing it path. If you do not want to use CLASSPATH you can use any other means to obtain a path (Ex: parameters, properties, etc) and load manually your classes.
But, CLASSPATH is a way to do things in a decoupled manner.
With the spread use of containers like Docker, we will probable face less problems, like class duplicity, that can occur when we use this CLASSPATH.
You don't need to set PATH or CLASSPATH.
Setting the PATH is just to make it easier to run the java command. You can always run it using fully qualified name, in which case the PATH is not needed.
Setting the CLASSPATH is never needed, but is sometimes easier than using the -cp option. If you run using -jar, the CLASSPATH is not used at all, and if you don't use -jar, the CLASSPATH is by default the current directory, so if your code is there (i.e. your package hierarchy is rooted there), it's not needed either.
I have a Java application that relies on some legacy code written in C, which it accesses via JNI. The native libraries are stored in a sub-directory (actually, there are two sub-dirs: one with 32-bit libraries and one with 64-bit).
In Windows, the following command line form works fine:
java -Djava.library.path=nativelib64 -classpath myapp.jar;jni_lib.jar MyApplication
but the equivalents on AIX and Linux (basically with the semi-colon in the classpath replaced with a colon) are failing with UnsatisfiedLinkErrors - I'm having to resort to setting the LIBPATH or LD_LIBRARY_PATH explicitly before the call, as nothing I've tried setting -Djava.library.path to (e.g. "nativelib64", "./nativelib64", "/full/path/to/myappdir/nativelib64") seems to be working.
Do the UNIX versions of the JVM not support setting the java.library.path property from the command line?
See http://kalblogs.blogspot.co.uk/2009/01/java.html:
java.library.path only works to resolve the immediate native library that you are loading in your code.
If this immediate library depends on other libraries (either within the same path as defined by java.library.path, or in a different location), these other libraries are loaded through the OS's standard mechanisms. In this case, it is also necessary to setup LD_LIBRARY_PATH accordingly.
You need to use 'LD_LIBRARY_PATH' variable.
LD_LIBRARY_PATH: native code libraries (on Linux, in addition to the
value of this variable, the lookup path typically contains
/usr/local/lib, /usr/lib, /lib and a few others). The name LD comes
from dynamic loader, the system component that loads libraries into
dynamically linked executables.
EDIT:
Check this link : http://www.chilkatsoft.com/java-loadLibrary-Linux.asp
Is it possible to use Eclipse's classpath variables within in Java code? I have several bin directories for multiple projects that are defined in classpath variables, and need to reference them multiple times within one of my projects.
Tying something unique to an IDE to your application code is not particularly good practice. Try instead to make use of system properties, environment variables or the -D arguments to the application.
I'm confused in understanding, how java interpretor and java compiler searches for all the necessary jar files it requires from environment variables. As I have only set the set path variable for JDK directory, but I've not set any variable to search for any class libraries, which jvm requires. How can it search those important jar files?
Which jar files are you talking about? Java already knows about the jar files it "owns" (such as rt.jar) - you don't have to tell it about them explicitly. This is known as the bootclasspath - you can override it, but usually you don't want to.
For better understanding of how classes are found and loaded by JVM read How Classes are Found.
CLASSPATH is an enviromental variable is like the path file (which helps windows to find executables). It lists a set of all places the JVM looks for classes. You can also give the classpath on the command line when starting the jvm and java compiler