Setting Java CLASSPATH

48,983

Solution 1

I'm not a Java programmer, so I don't know the correct value for CLASSPATH. But, you seem to. You can add it to ~/.bashrc like so:

CLASSPATH=/path/to/1:/path/to/2:/etc

The change will take effect globally the next time you log in. However, it will take effect immediately in new shells.

Additionally, if you want to set it for just one particular command, do this:

CLASSPATH=/something command-here arg1 arg2

A third way would be to create a wrapper script, which would be appropriate if you needed to set multiple variables or if you needed to determine appropriate values programmatically:

#!/bin/bash
export CLASSPATH=/something
export ANOTHER_ENV_Variable=foo

exec your_fancy_program "$@"

Solution 2

that's what happened with me.. i left empty spaces while typing.. it fixed when i removed spaces.

1) if you want to set classpath permanently then 1) find out where java is installed.. you may use " whereis java " openjdk-7/6 is in /usr/lib/jvm/.....

2) we need to set up CLASSPATH in /etc/environment

  sudo gedit /etc/environment

3) add the following likes .. ( DONT LEAVE ANY SPACES WHILE TYPING)(customize according to your java version and installation) (this home path is for open jdk 7)

  JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386/bin"

   export JAVA_HOME

   CLASSPATH=".:/usr/lib/jvm/java-7-openjdk-i386/lib:/home/laptop/Desktop/a2"

    export CLASSPATH

separate directory by ":"

Share:
48,983

Related videos on Youtube

Frank V
Author by

Frank V

Software Engineer, professionally - I work face-to-face with clients on a variety of software technologies (React, Node, .NET, PHP, MySQL, JavaScript, Python, etc). In addition, I have a particular affinity for open-source software. I study it to learn and make contributions when possible. I document my adventures at http://theOpenSourceU.org/ #SOreadytohelp

Updated on September 18, 2022

Comments

  • Frank V
    Frank V over 1 year

    I've done java development before but generally on windows (but I'm by no means an expert).

    I just want to set my CLASSPATH. When I run:

    echo $CLASSPATH

    (Is that right)? I get a empty line. So, it appears to me that my computer does not have a "Classpath" setup. What is the best practice to setup the classpath in Ubuntu (and linux in general)?

    I'm using the default java for Ubuntu which should be OpenJDK. Not sure it's important, but I want to use Rhino (http://en.wikipedia.org/wiki/Rhino_(JavaScript_engine)) Note: I've been able to install Rhino via the package manager. There is both documentation and rhnio packages. Then, the commands are js and rhino-debugger

  • Frank V
    Frank V over 12 years
    You are right, this would work but I guess what I'm after is what is the standard way to set the classpath. I think, in fact, there are several ways to set environment variables in Linux. This is probably the "problem" I face. What I seek is the "standard" way to do this, de facto or otherwise...
  • Scott Severance
    Scott Severance over 12 years
    @FrankV: See my edit above. There's no such thing as a single "standard" way to set environment variables in Linux. I've posted the three most common ways. You should choose the method that makes the most sense for your situation. A single "standard" would be too limiting and not useful.
  • Lekensteyn
    Lekensteyn over 12 years
    To set the classpath, you can also use the -cp option, e.g. java -cp /path/to/class/path name
  • michael
    michael over 11 years
    +1 on not using env vars, but rather using the command-line options. For more context, see my answer here on the same subject: askubuntu.com/questions/186693/…