开发者

How do I include java stuff in .jar files?

Okay. So here's my question: I am making a data parser in Clojure. One part of my program is that it has to be able to graph the data. I figure, I'll use jFreeChart. However, I have absolutely NO IDEA how to include stuff in JAR files. What I mean is: if I have a app.jar file in my classpath, I don't seem to be able to do:

import app.thing.thing2

without changing the classpath to be inside the jar file.

The idea here is that I don't think I can change my classpath since I need to set it to run Clojure (Or do I?). The global classpath is currently /usr/share/java.

And please don't ask me to use Maven, Ant or any project-building tool unless it is the only way to do this. This is a script for personal use that doesn't ne开发者_运维百科ed or want a whole lot of overhead.

I wonder if I should just unpack every JAR file, so that I can reference the directory structure? Is this bad?

Let me know if you need any clarifications!


The content of the (Java) CLASSPATH environment variable is available to Clojure so if you add your jar to the global classpath before to run Clojure, you'll "see" it:

export CLASSPATH=/path/to/jfreechart.jar:$CLASSPATH

But, in my opinion, this is not the "clean" way to add a jar to Clojure's classpath (because this makes the library visible to any Java program and may not be desired). Instead, you should use the CLOJURE_EXT environment variable. This is how this variable is documented:

# CLOJURE_EXT The path to a directory containing (either directly or as
# symbolic links) jar files and/or directories whose paths
# should be in Clojure's classpath. The value of the
# CLASSPATH environment variable for Clojure will be a list
# of these paths followed by the previous value of CLASSPATH
# (if any).

On my system, it is defined as below:

export CLOJURE_EXT=~/.clojure

So, to add jfreechart.jar (or any other library) to Clojures's classpath, copy it (or add a symlink pointing to it) in the directory defined in the CLOJURE_EXT variable.

And by the way (I'm sorry but your question is not that clear), if you want to bundle some Java classes into a jar, the command is something like that:

$ jar cf myjarfile *.class 

You'll find documentation of jar - the Java Archive Tool - here.


I completely respect your desire not to use a project management tool, though I just spent longer typing this sentence than it takes to set up leiningen. For your one-off script any tool is going to be overkill and Pascal Thivent's answer covers this very well. For people reading this question who perhaps want to produce a jar file, or easily load their Clojure into emacs/slime-swank I cant recommend leiningen too strongly.


If you going to basics you can inline your classpath to include the hardcoded location of your jars, so if you on windows it will look something like

java -cp .;%CLASSPATH%;C:/here/it/is/foo.jar com.foo.MyClass


Not sure how clojure is run, but don't you just add the jar file to the classpath?

i.e.

/usr/share/java:/home/user/myjarfile.jar
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜