3

This may be a silly question, but right now I have a rather large class that I want to use as a library. Where somebody can simply add this jar file to their classpath. And then simply do an import statement at the top, then he or she can start using this class.

Is there anything special I need to do or can I simply just use the jar file built?

3
  • 1
    Have you tried just using it? Commented Apr 9, 2010 at 14:45
  • That fact that this this is a single "large" class that you wish to use as a library sounds a bit alarming. Are these static utility methods? Typically you would distribute behaviour over multiple classes. Commented Apr 9, 2010 at 14:48
  • Yes they are static utility methods Commented Apr 9, 2010 at 15:00

4 Answers 4

6

a rather large class

Large classes don't usually make good libraries :)

Technically, yeah, all you need it put it in a JAR. To make it easy to use (including by yourself), you should spend some time to refactor it and break up the functionality into smaller classes according to the Single Responsibility Principle. For static utility methods, consider whether you can group them into several classes according to some theme, or perhaps even turn some of them into non-static methods (if there are situations where a client would use more than one of those methods on the same data, they're begging to become instance methods).

And you definitely should write Javadoc comments for all public classes and methods, and publish the Javadocs along with the code. A library without an API doc is almost useless.

Sign up to request clarification or add additional context in comments.

1 Comment

Just what I was thinking ... although it sounds like these are static utility methods in which case I guess it's ok to put them all in one class providing they're related of course.
2

You can simply put the JAR into the classpath and can import the class. No more magic needed.

Comments

1

You should just be able to use the jar, as is. All you need to ensure is that the class files in the JAR are in the correct directory structure (according to their package names)

Class Foo in package bar -> bar/Foo.class in the jar

Comments

1

create a JAR of .class files of your "large class" and then put that JAR in your CLASSPATH. Then you should be able to import the classes in the JAR via "import" statements. If you anticipate too many huge classes/libraries, check out the JVM parameters -Xms and -Xmx that takes care of heap usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.