简体   繁体   中英

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 need 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.

如果您要了解基础知识,可以将您的类路径内联到包含您的罐子的硬编码位置,所以如果您在Windows上它看起来像

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?

ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM