I was just reading this line:
The first thing the format() method does is load a Velocity template from the classpath named output.vm
Please explain what was meant by classpath in this context, and how I should set the classpath.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file:
Or sometimes you ‘bulk import’ stuff by saying:
So later in your program when you say:
The Java Virtual Machine will know where to find your compiled class.
It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. This is done by putting folder and jar files on your classpath.
Before we talk about how the classpath is set, let’s talk about .class files, packages, and .jar files.
First, let’s suppose that MyClass is something you built as part of your project, and it is in a directory in your project called
output. The .class file would be atoutput/org/javaguy/coolframework/MyClass.class(along with every other file in that package). In order to get to that file, your path would simply need to contain the folder ‘output’, not the whole package structure, since your import statement provides all that information to the VM.Now let’s suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put
lib/CoolFramework.jarinto your classpath. The VM will look inside the jar file for theorg/javaguy/coolframeworkpart, and find your class.So, classpaths contain:
How do you set your classpath?
The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:
On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.
The second way is to use the
-cpparameter when starting Java, like this:A variant of this is the third way which is often done with a
.shor.batfile that calculates the classpath and passes it to Java via the-cpparameter.There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (
:) is the classpath separator. On Windows the separator is the semicolon (;)So what’s the best way to do it?
Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. You change the
CLASSPATHenvironment variable so one program works, and you end up breaking another program.The
-cpis the way to go. I generally make sure myCLASSPATHenvironment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren’t happy when the global classpath is empty though – I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools).