• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reusing files  RSS feed

 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a number of methods that I would like to use in various programs I am working on. I have been using a number of IDEs: JCreator, Netbeans, Notepad and have just started poking around in Eclipse 3.1, but I am not truly proficient with any of them. I am half-decent when it comes to data structures and algorithms, but am very much stuck in a fairly straight-forward, simplistic schoolbook aproach to coding - I write pretty much everything from scratch - and not at all up to date on "real-world" use of Java. I'm unclear about the mechanics involved in using files/libraries I'm not writing myself...

I am wondering how I can set this up so I don't have to copy and paste the methods into each new project I make. After all, part of the beauty of Java is the reuse of code, right? Can I create a utilities class containing all the methods, place this in a folder somewhere and then include the the folder in the classpath? And then would I have to use an import statement? And if I'm on the right track, could anyone please write a paragraph describing this process. I'm not quite sure how to do this in practice.

Thanks in advance to anyone who has time to enlighten me

[ July 19, 2005: Message edited by: Flo Powers ]
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, you can. And that is exactly the purpose of libraries. When you have the time, you can code all your commonly-used functions and put them into a library (compile and package them into a jar file) and then include this file in the classpath. Next time when you need those, just import that jar into your file and use it, it will work fine. Let me know if you want a more detailed example.

BTW, do take a look at Jakarta Commons before starting to code your own utilies!

_steve.
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow - that's a quick reply!

The methods I mentioned are prescribed IO methods for a course I am involved with. I just tried putting them into a class and compiled it. No probs. Is it the .class file that goes into the JAR file? Let's say (for simplicity's sake) I place the JAR file called Foo directly on the C:/ drive. What would be the addition to the classpath, and how would the import statement look in a program where I wanted to utilize this library file?

Thanks!

PS. The Jakarta Commons project looks interesting - I guess the name derives from being the capital / main city on the island of Java?
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, am a bit stuck, actually. I have created the class and placed it in a jar file. The jar file is inside a folder specified in the classpath, but when try to build a program that imports the jar file, it cannot find it. This driver program is not placed in the same location but on the desktop.

I would have thought it should be able to find the jar file if it was located immediately inside a folder specified in the classpath? Maybe the syntax is wrong? What would the import statement look like if the jar file is called xyz, and it contains class XYZ?
[ July 19, 2005: Message edited by: Flo Powers ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The jar file name is not important, so long as you have added it to your cLasspath (and you should be able to just add the folder the jar file is in). Just import the class by its package name, the compiler will then look through every class and jar file in its classpath to see if it can find the class specified. Remember when you create your jar, if the class is in some sub-folder (as it probably will be if you have put in in a package) that folder structure needs to be reflected in the jar file.
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you have a single .class file, don't even bother JARring it. Just put it in a directory and include the full path to that directory in your classpath while compiling and executing that program.

_steve.
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, got it working.

Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!