Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Setting path and classpath

  1. #1
    Join Date
    Apr 2011
    Location
    Hyderabad
    Beans
    177
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Question Setting path and classpath

    Hi all,I am lil bit new to Linux platform.Can someone kindly guide me how to set path and classpath in Ubuntu 10.10..

    In windows we go to environment variables and create a new variable path for setting path and classpath for classpath

    Code:
    Variable:PATH
    Value   :C:/Program Files/Java/Jdk1.6.0/bin
    Code:
    Variable: CLASSPATH
    Value   : .;C:/oracle/product/db_1/jdbc/lib/classes12.jar
    The above is the format we set path and class-path in windows..so,how do we do the same thing back here in Ubuntu10.10

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Setting path and classpath

    You probably don't need to. Assuming you have installed the JDK from the repositories, a simple example will work out of the box:

    Code:
    $ cat Hello.java
    public class Hello {
        public static void main(String ... args) {
            System.out.println("hello");
        }
    }
    $ javac Hello.Java
    $ java Hello
    hello
    The PATH is already set up (via /usr/bin/java and a couple more steps via /etc/alternatives).

    If you need to compile or run with additional libraries, you can use the -cp option, e.g.

    Code:
    $ javac -cp <path-to-jar-file>
    $ java -cp <path-to-jar-file>
    Setting a CLASSPATH variable can just lead to confusion.

    If you are using an IDE, it will define its own settings for its Java platforms and libraries (i.e. classpath). Maven will pick up javac and java from the standard path and manage its classpath.

    So no need.

  3. #3
    Join Date
    Apr 2011
    Location
    Hyderabad
    Beans
    177
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Question Re: Setting path and classpath

    As u say,for normal java program path will be set by Ubuntu..But,coming to server side programming like servlets and jsp's and jdbc programming we need to set path and classpath for few jar files..

    For eg:

    Code:
    import java.sql.*; 
    class PureConnection 
    { 
    public static void main(String[]args) throws Exception 
    { 
    String driver = "oracle.jdbc.driver.OracleDriver"; 
    String url = "jdbc:oracle:thin:@localhost:1521:orcl"; 
    String user = "scott"; 
    String password = "tiger"; 
    Class.forName(driver); 
    System.out.println("Driver Loaded"); 
    Connection con = DriverManager.getConnection(url,user,password); 
    System.out.println("connected"); 
    con.close(); 
    } 
    }
    sandy@ubuntu:~/Java Programs$ javac PureConnection.java
    sandy@ubuntu:~/Java Programs$ java PureConnection.java
    Exception in thread "main" java.lang.NoClassDefFoundError: PureConnection/java
    Caused by: java.lang.ClassNotFoundException: PureConnection.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 21)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 66)
    Could not find the main class: PureConnection.java. Program will exit.
    sandy@ubuntu:~/Java Programs$
    For the above program to execute we need to set classpath for classes12.jar available in oracle(C:/oracle/product/db_1/jdbc/lib/classes12.jar).So,how can i set the classpath for that

  4. #4
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Setting path and classpath

    As I said above, you can use use the -cp option.

    If you must use the CLASSPATH environment variable, and I would advise against it:

    Code:
    export CLASSPATH=".:/home/sandy/Somewhere/classes12.jar"
    That will only exist for your current shell session. To make it permanent, add that line to ~/.bashrc or, preferably, a script called from ~/.bashrc

    If you are doing server-side programming with JSP and JDBC, consider the attached screenshot. These are the included libraries in a small web application written with Spring and Hibernate. If this is your direction, you could save yourself a lot of time and trouble by spending two hours working through the Maven tutorial.

  5. #5
    Join Date
    Apr 2011
    Location
    Hyderabad
    Beans
    177
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Setting path and classpath

    This is how my bash profile looks..where should i add that environment variables for setting the path and classpath in the below bash profile

    Code:
    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    
    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    
    # don't put duplicate lines in the history. See bash(1) for more options
    # ... or force ignoredups and ignorespace
    HISTCONTROL=ignoredups:ignorespace
    
    # append to the history file, don't overwrite it
    shopt -s histappend
    
    # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
    HISTSIZE=1000
    HISTFILESIZE=2000
    
    # check the window size after each command and, if necessary,
    # update the values of LINES and COLUMNS.
    shopt -s checkwinsize
    
    # make less more friendly for non-text input files, see lesspipe(1)
        [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
    
    # set variable identifying the chroot you work in (used in the prompt below)
    if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
        debian_chroot=$(cat /etc/debian_chroot)
    fi
    
    # set a fancy prompt (non-color, unless we know we "want" color)
    case "$TERM" in
        xterm-color) color_prompt=yes;;
    esac
    
    # uncomment for a colored prompt, if the terminal has the capability; turned
    # off by default to not distract the user: the focus in a terminal window
    # should be on the output of commands, not on the prompt
    #force_color_prompt=yes
    
    if [ -n "$force_color_prompt" ]; then
        if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
            # We have color support; assume it's compliant with Ecma-48
            # (ISO/IEC-6429). (Lack of such support is extremely rare, and such  # a case would tend to support setf rather than setaf.)
            color_prompt=yes
        else
            color_prompt=
        fi
    fi
    
    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
    unset color_prompt force_color_prompt
    
    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
       ;;
    esac
    
    # enable color support of ls and also add handy aliases
    if [ -x /usr/bin/dircolors ]; then
        test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
        alias ls='ls --color=auto'
        #alias dir='dir --color=auto'
        #alias vdir='vdir --color=auto'
    
        alias grep='grep --color=auto'
        alias fgrep='fgrep --color=auto'
        alias egrep='egrep --color=auto'
    fi
    
    # some more ls aliases
    alias ll='ls -alF'
    alias la='ls -A'
    alias l='ls -CF'
    
    # Add an "alert" alias for long running commands.  Use like so:
     #   sleep 10; alert
    alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
    
    # Alias definitions.
    # You may want to put all your additions into a separate file like
    # ~/.bash_aliases, instead of adding them here directly.
    # See /usr/share/doc/bash-doc/examples in the bash-doc package.
    
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    
    # enable programmable completion features (you don't need to enable
    # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
    # sources /etc/bash.bashrc).
    if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
        . /etc/bash_completion
    fi

  6. #6
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Setting path and classpath

    Quote Originally Posted by sandy0594 View Post
    This is how my bash profile looks..where should i add that environment variables for setting the path and classpath in the below bash profile
    Ah well, I tried.

    Anywhere (but not inside an 'if' block, obviously). At the bottom is probably easiest to find later on. You'll need to log out and back in again to see them appearing in new terminal windows.

  7. #7
    Join Date
    Aug 2009
    Beans
    124

    Re: Setting path and classpath

    well rightly said above if you have installed JDK it will set the PATH by itself
    and its better not to create ambiguity with CLASSPATH

    To see everythin is installed fine..check

    $>which java
    $>echo $PATH

    if /usr/bin/java is not in your path( and you are sure its installed ), just export the path using:
    export PATH=$PATH;/usr/bin/java

  8. #8
    Join Date
    Apr 2011
    Location
    Hyderabad
    Beans
    177
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Setting path and classpath

    I set the classpath and my java program is executing..But,i do have a small problem that needs to be addressed.I have 2 users in Ubuntu..One is sandy and oracle..In oracle user,oracle is installed and when i set the classpath in oracle user,the program executes.But,back in sandy user the program fails in execution phase.

    Code:
    export CLASSPATH=".:/opt/oracle/oracle/product/10.2.0/db_2/jdbc/lib/classes12.jar"
    In user sandy
    Code:
    sandy@ubuntu:~/Java Programs$ javac PureConnection.java
    sandy@ubuntu:~/Java Programs$ java PureConnection
    Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at PureConnection.main(PureConnection.java:10)
    In user oracle
    Code:
    oracle@ubuntu:/media/SANDY$ javac PureConnection.java 
    oracle@ubuntu:/media/SANDY$ java PureConnection 
    Driver Loaded 
    connected
    So,any hunch on how to solve this issue

  9. #9
    Join Date
    Aug 2009
    Beans
    124

    Re: Setting path and classpath

    looks like your user has some priviledge issue.
    the user 'oracle' has those priviledges.
    not very sure, but try to check it and see the link below



  10. #10
    Join Date
    Apr 2011
    Location
    Hyderabad
    Beans
    177
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Setting path and classpath

    As per my programming knowledge,the problem is regarding the environment variables..Its just that the environment variable specified in user oracle should be used by user sandy.How to use the environment variable specified in one user could be used in other user is the point

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •