• Post Reply Bookmark Topic Watch Topic
  • New Topic

Importing external libraries such as apache commons  RSS feed

 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently using TextPad or command prompt to compile and run my programs, and as usual I am not having any luck importing external libraries, in particular apache commons.

I couldn't see any particularly obvious settings in TextPad to set a library or class path (which is annoying as I really like the program), so I jumped back to cmd prompt to run it from there. Aaaaaand i don't understand .

This is my file structure at present:

I'm navigating to the /mainProject folder in cmd prompt and typing (amongst many other things)

i've tried pointing it at the root folder where the import statement begins in the code (ie mainProject/commons-lang3/apidocs), tried moving the .jar files to the mainProject folder (pointing -cp at it) and more. No luck.

Does anyone know of a link to a really good explanation of this? it seems everytime i try to import a new library something goes wrong..... its become a bit of a stumbling block to be honest. I seem to spend most of my time dealing with the 'administrative' side of coding (that was the politest way i could put it ), am I not doing it right?

Thanks in advance

Nick


** I've read I'm supposed to include the .jar in the classpath, not just the directory. is this correct? (it still doesn't work...!)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,
Yes, you want to include the jar by name. Another missing part is that you should include . (the current directory) in your classpath.

On Unix:
javac -cp ".:/mainProject/commons-lang3/commons.jar" MonsterGame.java

On Windows
javac -cp ".;/mainProject/commons-lang3/commons.jar" MonsterGame.java
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*edit: sorry, i'm still a little confused. it's still saying that the library doesn't exist.

javac -cp ".;/mainProject/commons-lang3/commons-lang3-3.3.2.jar" monstergame.java //where main project is the current directory and the .jar is in that folder

i'm getting the error:

monstergame.java:1: error: package org.apache.commons does not exist import org.apache.commons.lang3;


 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for some reason it was commons-lang3-3.3.2.javadoc.jar, not commons-lang3-3.3.2.jar. both were unzipped in the commons-lang3 folder.

excellent thank you very much jeanne!


*edit: nope! it seems to have gone wrong again. i think i'm tired! will try again in the morning!!!
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the repost guys, I'm still struggling!

I've the external .jar in the folder projectMain/commons-lang3 (as i extracted the zip file to the mainProject folder)

The class definitions appear to be in commons-lang3/apidocs/apache/commons/lang3 (again, as extracted)

I've navigated to the mainProject folder in cmd prompt and typed:

javac -cp ".;/mainProject/commons-lang3/commons-lang3-3.3.2-javadoc.jar" monstergame.java
(or -cp ".;/mainProject/commons-lang3/commons-lang3-3.3.2.jar" - there are two .jar files included in the extract)

and I'm receiving the error "1: error: pacake org.apache.commons.lang3 does not exist"
and then it is pointing to the import in the file, 'import org.apache.commons.lang3 ^ ArrayUtils;' (where the '^' is)

I'm genuinely stumped, I don't want to go back to an IDE, but its look more and more likely!

is it something obvious?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:for some reason it was commons-lang3-3.3.2.javadoc.jar, not commons-lang3-3.3.2.jar. both were unzipped in the commons-lang3 folder.

That is really strange. You would say that the jar with "javadoc" in the name would just contain the documentation. Maybe you saved the jars with the wrong names when you downloaded them.

You don't need to unzip jar files. Just put the jar files themselves in the classpath with the "-cp" option.

What operating system are you using - Windows, Mac OS X or Linux? Note that on Windows, elements in the classpath are separated by ";" (semi-colon) and on Mac OS X and Linux by ":" (colon). So if it is Mac OS X or Linux, use colon:

java -cp .:/mainProject/commons-lang3/commons-lang3-3.3.2.jar com.mypackage.MyProgram

(Is the jar file really in the directory /mainProject/commons-lang3 ? That seems an unlikely place if you're on a Unix-like OS).
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadoc file is irrelevant for the classpath; don't include that.

/mainProject/commons-lang3/commons-lang3-3.3.2.jar

You almost certainly do not have a root directory called "mainProject", so using an absolute path like that is wrong. Maybe you meant "mainProject/commons-lang3/commons-lang3-3.3.2.jar" instead?
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys.

@Jesper: the file I'm extracting is the download zip 'commons-lang3-3.1-bin.zip' from the page http://commons.apache.org/proper/commons-lang/download_lang.cgi. The .jar is being left untouched

@Ulf: yeah that sounds likely. Is the 'root' folder not where I've navigated to in cmd prompt? Maybe that's the mistake I'm making, hang on, I'll try the full path from c:\



*edit: that appears to solve the issue with the commons-lang3 library, but it breaks my monster.class in the mainProject folder. Monstergame.java now won't compile because Monster.class cannot be found. I'm assuming because the classpath now points to the folder containing the library, which is one level below the mainProject where my class files are.

I'm going to try moving the .jar to the same folder as my classes. *edit: (still complains about my monster class but seems fine with the commons-lang3.jar)
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you start including external libraries in your projects, you should consider using a Java build tool such as Maven or Ant. They can simplify this sort of task.

Originally, I used Windows batch files or even Makefiles, but Maven and Ant are more closely attuned and they understand better how to not do unnecessary things, so they're often faster.

If that isn't enough incentive, just about every commercial development shop is going to be using them, so familiarity with those tools can make you more hireable, if you're planning a career in Java development.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:*edit: that appears to solve the issue with the commons-lang3 library, but it breaks my monster.class in the mainProject folder. Monstergame.java now won't compile because Monster.class cannot be found. I'm assuming because the classpath now points to the folder containing the library, which is one level below the mainProject where my class files are.

The classpath can point to any number of places. Earlier you had one that included "." (the current directory) which probably was where your own classes could be found. If you wish the classpath to be valid if you change directories, you need absolute paths for everything, like "-cp /this/is/the/path/to/commons/lang.jar:/this/is/the/path/to/my/class/directory". Note what Jesper said about differences between operating systems.

+1 to what Tim said about using a build system. For my own projects, as soon as there is at least one library involved, I generally set up an Ant build file for it.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I'm going to have to introduce you guys to the mess that is my folder structure. mainProject was just a place holder to save me embarassment, and you guys headache



so assuming that I'm pointing -cp to the commons-lang3 folder (where the commons-lang3-3.3.2.jar is, as opposed to the copy in the lessonseven folder) I would type:

this works for the external library. but obviously the classpath now doesn't point to the lessonseven folder where my monster.class and monstergame.class reside. so I get the error 'cannot find symbol' Monster. So I then try putting a copy of the commons-lang3-3.3.2.jar file where my classes are, assuming then all the classes are together.

that still breaks my monsters class - presumably because the cp is still pointing at the jar, not the folder.

so I've tried adding two classpaths
(with various combinations of '.;' ';' and quotations marks dividing the two paths)

This still gives me an error on my monster class, but at least it now goes into further detail: "the system cannot find the path.... make sure its in the correct subdirectory" - this is in reference to my Monster.class, not the external library.



@Tim - good advice, project structure is, as you can see(!), something i need to work on! but for the time being I want to at least understand the basics sans IDE and the like But yes, as soon as that is done I'm going to jump on that suggestion. as you said, it can't hurt to know it when looking for jobs and it will undoubtedly make my life easier! I don't deserve it. yet


Thanks again for all your time, much appreciated!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac -cp "C:/users/beast/javatextprojects/textpad/javavideotutorial/lessonseven_classes(monster)/commons-lang3/commons-lang3-3-3.3.2.jar .;c:/users/beast/javatextprojects/textpad/javavideo tutorial/lessonseven_classes(monster)" monstergame.java

Getting closer, but there's an extra space and extra dot that have no business being there. I also advise not to use spaces or any characters like parentheses in paths.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
javac -cp "C:/users/beast/javatextprojects/textpad/javavideotutorial/lessonseven_classes(monster)/commons-lang3/commons-lang3-3-3.3.2.jar .;c:/users/beast/javatextprojects/textpad/javavideo tutorial/lessonseven_classes(monster)" monstergame.java

Getting closer, but there's an extra space and extra dot that have no business being there. I also advise not to use spaces or any characters like parentheses in paths.




Yeah, I've seen that dot in many examples, so threw it in for the hell of it! (that's the stage it's got to!)

*(&(*&&*() GET IN!

it was the space!! (and the dot, but I'd tried without several times!)

THANK YOU!!

*edit: just out of interest, does the dot refer to the root directory? what is my root determined by? i run the compiler from the lessonseven folder, can i just put -cp "./commons-lang3/;./" or something similar (to avoid typing the whole path?)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dot refers to the current directory (whatever the current directory is when you are in the command prompt).
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Dot refers to the current directory (whatever the current directory is when you are in the command prompt).


Hi Jesper.

I guess what confuses me is that ';' divides elements in the classpath, and '.' refers to the current directory in the element.... however......

i see '.;' and '.:' all the time. but surely it should be ';.' or ':.' as the dot should be part of the path, and the semi colon should divide the elements in the path.

for example:
java -cp "<fullPath>/lessonseven_classes(monster)/commons-lang3/commons-lang3-3.3.2.jar;./commons-lang3-3.3.2.jar" com.mypackage.MyProgram

no?

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dot can start a path, but it is also an entire path by itself.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:The dot can start a path, but it is also an entire path by itself.


Thanks, am going to go mess around with this now .

Cheers for all the help everyone, really has been something that I've found lacking in the text books and video series I'm using. love this place


*edit: just in case I don't come back to this i thought I'd share this as well: http://kevinboone.net/classpath.html - really has explained to me why I'm not getting the results I expect




 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, hundreds of years ago it used to be that people learning how to use computers started by learning what the current working directory was, and what a parent directory was, and all that sort of thing. Now they are just handed a computer with a bunch of functional applications and somehow they are just supposed to know that sort of thing without being told.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!