Use Eclipse classpath variables in Java code - java

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.

Related

Why is it needed to set path and CLASSPATH FOR JDK in environment variables only?

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.

Use Variables during deployment in Eclipse

I do have some configuration files. Currently these files are completely written manually. But some parts do change from time to time, like version number or current year.
In my ant scripts I could define some variables within the config files and replace these variables during my deployment with the current values. But I do use ant for the full automated nightly build. For normal and interactive work with the software, I do use Eclipse directly without any ant target.
Does anyone know any way how to replace such variables also within Eclipse deployment?
Of course, I could keep my source-config file with variables and use ant to create the final config file also in the source folder which is deployed and used, so both are checked in. But this is also redundant information and looky like a hack to me!
Because of so many misunderstandings:
I would like to have a source-config file with e. g. #MY-VAR# within the text. During deployment, this placeholder is replaced by a variable value known at compile time. This is possible with ant. Can I do something similar with Eclipse?
How do you read the variables? I would go for system wide properties, this will also work on Linux and Windows platforms and can be changed from computer to computer.
Are you developing in Java ?
Would this be helpful ?
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_run_env.htm

PHP Environment variable

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.

How should I set CLASSPATH?

I did this before:
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/tools.jar:/home/phoenies/jdk1.6.0_17/lib/dt.jar"
But today an article says I should do this:
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib"
If I do so, will it search all the jar files in lib? So it's probably a shorter way?
Since you are using JDK6, you can use classpath wildcards: CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/*" will match all JARS inside lib/
Check out http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html there's a section called "Understanding class path wildcards"
I think having a CLASSPATH environment variable is wrong for all but the easiest of "Hello, World" tutorials.
The right way is to set the CLASSPATH for every project when you compile and run. Every project is likely to be different, so this makes perfect sense.
IDEs ignore CLASSPATH environment settings; so do all Java EE app servers. It's a relic of Java 1.0. I don't have CLASSPATH set on any machine that I work on.
Learn to script it for the command line. Or use Ant. You'll be glad you did.
Yes, it will search all jar files in lib if you do it the second way. It's pretty odd to see class path being set as specifically as in the first one. I suppose on a server where you wanted to be sure what jars were being loaded, that might be one way to restrict them, but you might run into issues with how long it can be if you had several jars.
Jar files need to be specified by name in the Classpath variable. One thing to note is that the commandline -classpath param is more versatile than the environment variable, as it allows you to set a classpath per application.
In Java 1.6+ you can set the classpath to a directory followed by /* to load all JAR files in that directory. Not just the directory name though - that's for loading class files in that directory and subdirectories.

Confusion in understanding classpath

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

Categories