« back

The Java Classpath - What the He** is it?

28 Feb 2013

After pointing a fellow apprentice to my post about getting your sdk set up with intellij, I figured he'd be set. Well, it turns out that setting up junit can be a PITA for others as well, and for different reasons. While teaming up with him to work this out, I tried to explain that he needed to set his CLASSPATH in his bash profile. As I was "explaining" it to him, I realized that I only had a vague idea of what that means. Let's take a closer look.

The PATH and CLASSPATH are environment variables on your machine. Cool. What does that mean? Let's ask wikipedia!( I'll give you the juicy bits but click here if you want to read it all for yourself.)

Environment variables are a set of dynamic values, that are named, that can effect the way that running processes on your computer will behave. In all Unix and Unix-like systems, each process has it's own separate set of environment variables. Here is a very important part...By default, when a process is created it inherits a duplicate environment of it's parent process, except for explicit changes made by the parent when it creates the child. (If you happen to be an apprentice at 8th Light when I wrote this, some of this may be starting to sound familiar. Josh Cheek gave a great talk on the ruby load path in one of our apprentice learning hours.) From a command shell, a user can change environment variables for a particular command invocation by indirectly invoking it via env or using the ENVIRONMENT_VARIABLE=VALUE notation. ***This will not change it permanently, but while you are running that newly created process. Let's not get any deeper into this today but is important to know that in your bash profile, you can set environment variables that will be used every time you run a process. Enter the CLASSPATH.

The CLASSPATH is a parameter that you can set via an environment variable that tells the Java Virtual Machine or Java compiler where to look for user defined classes and packages. When we want to run a program, we give the name of the program to run but we also need to tell java where to look for the files and directories. This is machine dependent. If you are on linux, java always looks at the current working directory. From the command line, you may type something like,

1     java -classpath /home/user/Development/Apprenticeship/server  org.javaserver.Connection

You can alter the CLASSPATH environment variable in your bash profile or set the classpath in a manifest file(defined within your jar file.) This is generally what an IDE helps you out with auto"magically". You mostly do not even have to think about it. This adding the classpath to the manifest is the sugar that allows you to point to the jar for your classpath. With that you can say...

1 java -jar /home/user/Development/Apprenticeship/server/out/artifacts/javaserver_jar/javaserver.jar

You have likely seen this before.

Right about now you should be feeling a bit classier.

comments powered by Disqus