LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-26-2011, 03:20 AM   #1
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Classpath in bash


Hi all

I've got a question about the classpath, i know what it is and what it does, but in my script i have to add a lot of .jar files.
So my question is, can i write things easier (more clear, more manageable)?

Code:
CLASSPATH=/testmap_webdriver/:/testmap_webdriver/lib/apache-mime4j-0.6.jar:/testmap_webdriver/lib/cglib-nodep-2.1_3.jar:/testmap_webdriver/lib/commons-cli-1.2.jar:/testmap_webdriver/lib/commons-codec-1.4.jar:/testmap_webdriver/lib/commons-collections-3.2.1.jar:/testmap_webdriver/lib/commons-io-1.4.jar:/testmap_webdriver/lib/commons-lang-2.4.jar:/testmap_webdriver/lib/commons-logging-1.1.1.jar:/testmap_webdriver/lib/cssparser-0.9.5.jar:/testmap_webdriver/lib/guava-r08.jar:/testmap_webdriver/lib/hamcrest-all-1.1.jar:/testmap_webdriver/lib/htmlunit-2.8.jar:/testmap_webdriver/lib/htmlunit-core-js-2.8.jar:/testmap_webdriver/lib/httpclient-4.0.2.jar:/testmap_webdriver/lib/httpcore-4.0.1.jar:/testmap_webdriver/lib/httpmime-4.0.1.jar:/testmap_webdriver/lib/jna.jar:/testmap_webdriver/lib/json-20080701.jar:/testmap_webdriver/lib/junit-4.8.1.jar:/testmap_webdriver/lib/junit-dep-4.8.1.jar:/testmap_webdriver/lib/nekohtml-1.9.14.jar:/testmap_webdriver/lib/sac-1.3.jar:/testmap_webdriver/lib/selenium-java-client-driver.jar:/testmap_webdriver/lib/seltest.jar:/testmap_webdriver/lib/serializer-2.7.1.jar:/testmap_webdriver/lib/testng-5.14.1.jar:/testmap_webdriver/lib/xalan-2.7.1.jar:/testmap_webdriver/lib/xercesImpl-2.9.1.jar:/testmap_webdriver/lib/xml-apis-1.3.04.jar:/testmap_webdriver/lib/selenium-server-standalone-2.0b3.jar
you see there are A LOT of them , and I DO need them
I tried something like this:
Code:
CLASSPATH=/testmap_webdriver/:/testmap_webdriver/lib/*.jar
but that doesn't seem to work...

Can someone help me pls ^^

Thanks in advance

Brownie
 
Old 04-26-2011, 03:49 AM   #2
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
okay ... did something stupid...

in stead of
Code:
CLASSPATH=/testmap_webdriver/:/testmap_webdriver/lib/*.jar
->
Code:
CLASSPATH=/testmap_webdriver/:/testmap_webdriver/lib/*
so it works (leave the .jar out)

Hopefully some new guys will find this usefull
 
Old 04-26-2011, 04:05 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
I don't believe bash does any globbing expansion when setting variables.
Even if it did, it wouldn't come out colon-delimited.

As for this appearing to work:

Code:
CLASSPATH=/testmap_webdriver/:/testmap_webdriver/lib/*
I'm betting that you're using echo to check it, and not quoting the expansion.

Code:
echo $CLASSPATH     #Variable contains a glob pattern, which is
                    #expanded before echoing.

echo "$CLASSPATH"   #Globbing pattern is protected when echoed,
                    #so no expansion occurs.
Instead, you can use find or a similar command inside a command substitution to generate the list you want.
Code:
CLASSPATH=/testmap_webdriver/$( find /testmap_webdriver/lib/ -name "*.jar" -printf ":%p" )
This is assuming gnu find, which has the -printf option. Otherwise you'd have to add an extra step to reformat the output. Add -maxdepth 1 to the find command before the -name option if you don't want it to recurse into lower levels.
 
Old 04-26-2011, 04:50 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Thx

But are you saying that:
Code:
CLASSPATH=/testmap_webdriver/:/testmap_webdriver/lib/*
is not 100% safe?

I also try this one:
Code:
CLASSPATH=/testmap_webdriver/$( find /testmap_webdriver/lib/ -name "*.jar" -printf ":%p" )
i'll keep you posted
 
Old 04-26-2011, 04:54 AM   #5
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
okay, here is the feedback

when i'm using this:
Code:
CLASSPATH=${DIR}:${DIR}/lib/*
-> i get:
Code:
.:./lib/*     -> this is an echo
OK - be.vito.nagios.selenium.test.LoginLogout Test passed | ExecTime=690ms.
and when i'm using this:
Code:
CLASSPATH=${DIR}:${DIR}/lib/$( find /testmap_webdriver/lib/ -name "*.jar" -printf ":%p" )
-> i get:
Code:
.:./lib/:/testmap_webdriver/lib/cglib-nodep-2.1_3.jar:/testmap_webdriver/lib/junit-dep-4.8.1.jar:/testmap_webdriver/lib/xalan-2.7.1.jar:/testmap_webdriver/lib/junit-4.8.1.jar:/testmap_webdriver/lib/commons-collections-3.2.1.jar:/testmap_webdriver/lib/serializer-2.7.1.jar:/testmap_webdriver/lib/nekohtml-1.9.14.jar:/testmap_webdriver/lib/htmlunit-core-js-2.8.jar:/testmap_webdriver/lib/xml-apis-1.3.04.jar:/testmap_webdriver/lib/cssparser-0.9.5.jar:/testmap_webdriver/lib/jna.jar:/testmap_webdriver/lib/sac-1.3.jar:/testmap_webdriver/lib/seltest.jar:/testmap_webdriver/lib/json-20080701.jar:/testmap_webdriver/lib/commons-codec-1.4.jar:/testmap_webdriver/lib/commons-logging-1.1.1.jar:/testmap_webdriver/lib/apache-mime4j-0.6.jar:/testmap_webdriver/lib/selenium-java-client-driver.jar:/testmap_webdriver/lib/xercesImpl-2.9.1.jar:/testmap_webdriver/lib/guava-r08.jar:/testmap_webdriver/lib/hamcrest-all-1.1.jar:/testmap_webdriver/lib/httpcore-4.0.1.jar:/testmap_webdriver/lib/selenium-server-standalone-2.0b3.jar:/testmap_webdriver/lib/testng-5.14.1.jar:/testmap_webdriver/lib/httpmime-4.0.1.jar:/testmap_webdriver/lib/commons-io-1.4.jar:/testmap_webdriver/lib/httpclient-4.0.2.jar:/testmap_webdriver/lib/commons-lang-2.4.jar:/testmap_webdriver/lib/commons-cli-1.2.jar:/testmap_webdriver/lib/htmlunit-2.8.jar
OK - be.vito.nagios.selenium.test.LoginLogout Test passed | ExecTime=710ms.
So both works, but maybe with the 2nd one you are more safe ?
 
Old 04-26-2011, 05:28 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
What I'm saying is that it's not doing what you think it's doing, and is only coincidentally giving you what you want.

I can't replicate your behavior anyway. here's a simple test I just ran:
Code:
$ list=*.txt

$ echo "$list"
*.txt                #No expansion evident.  The actual
                     #contents of the variable are displayed.

$ echo $list
a.txt b.txt c.txt    #The *.txt in the variable is expanding to the
                     #contents of the directory just before echoing.

$ list=/home/david/:*.txt

$ echo "$list"
/home/david/:*.txt   #No expansion evident  The actual
                     #contents of the variable are displayed.

$ echo $list
/home/david/:*.txt   #No expansion evident  The actual
                     #contents of the variable are displayed.

$ IFS=:
$ echo $list
/home/david/ a.txt b.txt c.txt  #Setting IFS to colon allows expansion.
The final two results are interesting. They show definitively that bash can't read file globs in complex strings. But if I set the word separator to colon instead, then it breaks the string up into two separate paths, and can expand any globbing patterns before echoing them.

Whether all this is "safe" or not depends on how the variable is used. I'm only using echo here. Perhaps your java interpreter or whatever it is is more intelligent and does its own string expansion after it reads the variable.

In any case, find will certainly give you a more accurate output.
 
1 members found this post helpful.
Old 04-26-2011, 05:32 AM   #7
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
thanks for your reply !!
I used your suggestion -> find ... and so on !!

Quote:
only coincidentally giving you what you want
because of this reason, i'm using find because i want to be sure that all my .jar files are listed and used

thanks again !!
 
Old 04-26-2011, 06:03 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Oh, and I just noticed something. Please change this...
Code:
CLASSPATH=${DIR}:${DIR}/lib/$( find /testmap_webdriver/lib/ -name "*.jar" -printf ":%p" )
...to this:

Code:
CLASSPATH=${DIR}$( find ${DIR}/lib/ -name "*.jar" -printf ":%p" )
The %p in the format string I used already includes the search path in each file name, and :%p adds a colon to the front of them.

As general advice, whenever you aren't sure about what a complex command does, break it down and see what each individual part is doing. Try running the find command on it's own, for example:
Code:
find ${DIR}/lib/ -name "*.jar" -printf ":%p"
Imagine what you'd get by replacing $(..) in the variable setting with that output.
Then check it with this:

Code:
echo "${DIR}$( find ${DIR}/lib/ -name "*.jar" -printf ":%p" )"
If THAT gives you what you want, then you know you're safe to use it to set your CLASSPATH.
 
1 members found this post helpful.
Old 04-26-2011, 06:33 AM   #9
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Thanks man !! That did the job ^^
I changed it and echoed it and it works perfectly !!

Arrigato Gozaimasu

(don't know if i wrote it right )

Last edited by brownie_cookie; 04-26-2011 at 06:35 AM.
 
Old 04-26-2011, 07:09 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Eh, close enough.

Although more accurately you should use the past tense here.
arigatou gozaimashita (ありがとうございました)

And in response,
douitashimashite
(どういたしまして)

 
Old 04-26-2011, 07:49 AM   #11
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
nice
Are you originally from Japan or are you moved to Japan for work?

I'd like to learn japanese (i did karate and i'm very interested in samoerai and so on )
but in Belgium aren't much places where you can learn japanes though...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CLASSPATH in java abd_bela Programming 3 08-26-2008 06:43 AM
Java Classpath jadewarrior Red Hat 2 06-08-2006 01:12 AM
how to set CLASSPATH mohama Programming 8 12-17-2005 03:17 AM
Classpath Hiper1 Linux - Software 3 11-25-2004 09:18 AM
Swing 1.1 or 1.2 and CLASSPATH jboo Linux - Software 0 02-07-2003 11:35 AM


All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration