LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-15-2016, 09:17 AM   #1
Kali84
LQ Newbie
 
Registered: Mar 2016
Posts: 5

Rep: Reputation: Disabled
Adding jars to classpath


Hi folks, I'm looking to use a script to add multiple jar libraries to CLASSPATH for a java project. Easy for me with a bat in Windows but I can't seem to get it to work on my amazon instance.

This is my home directory

ec2-user> test1.jar test2.jar poi-3.14 jar.sh

Test1.jar works fine and has no library calls from poi. Test2.jar gives a no main exception. poi-3.14 is the directory containing the poi jar files. jar.sh is my script as printed below. I've tried a few different things now such as changing the jar to tar in the file endings. Adding the . , adding the\ after the : and nothing seems to be working. I'm getting the same error each time.

This is my script

Code:
#!/bin/bash
export CLASSPATH=/home/ec2-user/poi-3.14/poi-3.14-20160307.jar:\
/home/ec2-user/poi-3.14/poi-scratchpad-3.14-20160307.jar:\
/home/ec2-user/poi-3.14/poi-ooxml-schemas-3.14-20160307.jar:\
/home/ec2-user/poi-3.14/poi-examples-3.14-20160307.jar:\
/home/ec2-user/poi-3.14/poi-excelant-3.14-20160307.jar:\
/home/ec2-user/poi-3.14/poi-ooxml-3.14-20160307.jar:\.
Am I doing something silly here?
 
Old 03-15-2016, 09:48 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
What is the error message you got?
Probably you need another construct, without \<newline>:
Code:
D=/home/ec2-user/poi-3.14
CLASSPATH=$D/poi-3.14-20160307.jar:$D/poi-scratchpad-3.14-20160307.jar:poi-ooxml-schemas-3.14-20160307.jar
CLASSPATH=$CLASSPATH:$D/poi-examples-3.14-20160307.jar:$D/poi-excelant-3.14-20160307.jar:poi-ooxml-3.14-20160307.jar
export CLASSPATH
 
1 members found this post helpful.
Old 03-15-2016, 10:23 AM   #3
Kali84
LQ Newbie
 
Registered: Mar 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
What is the error message you got?
Probably you need another construct, without \<newline>:
Code:
D=/home/ec2-user/poi-3.14
CLASSPATH=$D/poi-3.14-20160307.jar:$D/poi-scratchpad-3.14-20160307.jar:poi-ooxml-schemas-3.14-20160307.jar
CLASSPATH=$CLASSPATH:$D/poi-examples-3.14-20160307.jar:$D/poi-excelant-3.14-20160307.jar:poi-ooxml-3.14-20160307.jar
export CLASSPATH
Thanks for the help pan64, I finally have it working. It took me way longer than it should have.
 
Old 03-15-2016, 10:45 AM   #4
Kali84
LQ Newbie
 
Registered: Mar 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Kali84 View Post
Thanks for the help pan64, I finally have it working. It took me way longer than it should have.
Seems I spoke too soon. The programme is running now, which is more than before, but when the library call executes I'm getting a NoDefClassFoundError org/apache/poi/poifs/filesystem/POIFSFileSystem error.

Also, when I run 'echo $CLASSPATH' I get a blank return.

Last edited by Kali84; 03-15-2016 at 11:20 AM.
 
Old 03-15-2016, 11:50 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
so you did not set it properly. I do not know what did you do, so hard to give a hint...
 
Old 03-15-2016, 11:57 AM   #6
Kali84
LQ Newbie
 
Registered: Mar 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
so you did not set it properly. I do not know what did you do, so hard to give a hint...
I uploaded the jar.sh file to my Linux server. Ran chmod +x jar.sh, then ./jar.sh. I then ran my test.jar (java -jar test.jar) which has the library calls but on executing the library call the error I mentioned occurred. Am I not running the script right. Should the classpath not print out when I echo $CLASSPATH?

Also I added in exec java -jar test2.jar. This starts my programme running. But when I have exec java -cp -jar test2.jar, I receive a could not loaf or find main class test2.jar error.

Last edited by Kali84; 03-15-2016 at 01:51 PM.
 
Old 03-16-2016, 02:39 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
was this CLASSPATH setting made in that jar.sh? ./jar.sh will start a new shell and CLASSPATH will be set inside that shell. The shell will be finished when the script completed and all the settings will be lost. The parent shell (your current one) will never know what's happened there.
If you want to set the current environment use:
source jar.sh
or
put java -cp -jar ... into that jar.sh
 
Old 03-16-2016, 07:51 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
"Ninety-nine jars of code on the wall ... ninety-nine jars of code ..."

When building long and complicated path-strings, I use a series of export-commands, each of which adds one new file-name to the string, and each of which fits neatly on one (short ...) line in the file. The script is executed as part of the bash-profile. It can also be executed by the scripts which invoke the Java environment to do things.

From time to time I've also encountered scripts that read a list of library-names from a file and assemble these into an environment variable.
 
  


Reply



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
Where to find the JARs for javax.swing.* and java.awt.* jlinkels Programming 5 01-02-2009 07:46 PM
how to add external jars, Java program with Reference Libraries yuubouna Linux - Newbie 3 11-30-2008 09:19 PM
common jars for all JSP applications in tomcat in linux raghuveervellanki Linux - Newbie 1 10-17-2008 01:28 AM
How to work with jars and Ant ???????????????????? manolakis Programming 2 03-23-2007 04:54 AM
Java and Jars ? EAD Programming 6 12-31-2006 02:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:31 PM.

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
Open Source Consulting | Domain Registration