【问题标题】:Pass a vector/inputstream to a clojure function via java/scala通过 java/scala 将向量/输入流传递给 clojure 函数
【发布时间】:2013-03-11 20:11:13
【问题描述】:

我需要从 java/scala 调用一个 clojure 函数,它需要一个向量或一个输入流作为它的第一个参数。

这样做总是会产生以下异常:

执行异常[[UnsupportedOperationException: pdf(clj-pdf.main/-pdf未定义?)]]

我正在使用clj-pdf,需要调用pdf函数

(defn pdf
  "usage:
   in can be either a vector containing the document or an input stream. If in is an input stream then the forms will be read sequentially from it.
   out can be either a string, in which case it's treated as a file name, or an output stream.
   NOTE: using the :pages option will cause the complete document to reside in memory as it will need to be post processed."
  [in out]
  (if (instance? InputStream in)
    (stream-doc in out)
    (write-doc in out)))

我已经修改了源码,通过

构建了一个jar
leiningen uberjar

最后2行可以看到对cjl-pdf的project.clj的修改:

(defproject clj-pdf 
  "1.0.6"
  :description "PDF generation library"
  :url "https://github.com/yogthos/clj-pdf"
  :license {:name "GNU Lesser General Public License - v 3"
            :url "http://www.gnu.org/licenses/lgpl.html"
            :distribution :repo
            :comments "same as  iText and JFreeChart"}
  :dependencies [[org.clojure/clojure "1.5.0"]
                 [jfree/jfreechart "1.0.13"]                 
                 [itext-min "0.2"]]

  :aot [clj-pdf.main]
  :main clj-pdf.main)

在我添加的 main.clj 中:

(ns clj-pdf.main
  (:gen-class
   ;; neither java.io.InputStream nor ArrayList work:
   :methods [#^{:static true} [pdf [java.util.ArrayList, java.io.OutputStream] void]])
  (:use clj-pdf.core))

(defn -main [& args])

我正在使用我的 scala 代码中的 lib,如下所示:

val output = new ByteArrayOutputStream()
val list = new java.util.ArrayList[String]
list.add( """[:list {:roman true} 
             [:chunk {:style :bold} "a bold item"] "another item" "yet another item"]
             [:phrase "some text"]                                   
             [:paragraph "yet more text"]]""")

clj_pdf.main.pdf(list, output)

有没有办法解决这个问题?

【问题讨论】:

  • 添加 -pdf 函数没有帮助,仍然抛出相同的 ex。

标签: scala clojure clojure-java-interop


【解决方案1】:

它适用于 Java:

    java.util.ArrayList a = new java.util.ArrayList();
    java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
    clj_pdf.main.pdf (a, b);

如果我添加到 main.clj:

 (defn -pdf [in out] (pdf in out))

并使用lein uberjar 构建项目。

另一种选择是按照https://stackoverflow.com/a/6410926/151650 使用clojure.lang.RT

【讨论】:

  • 我将-pdf函数添加到错误的文件中,谢谢提示!
猜你喜欢
  • 2019-03-12
  • 2015-07-18
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
相关资源
最近更新 更多