简体   繁体   English

如何在Clojure中使用jMusic(Java库)?

[英]How do use jMusic (a Java library) with Clojure?

I've decided I want to use Clojure's Java interoperability to play with the jMusic library. 我已经决定要使用Clojure的Java互操作性与jMusic库一起使用。 Because I know Java better than Clojure (by a lot), I'm testing things in Java and then rewriting them as (not-so-idiomatic) Clojure. 因为我比Clojure更好地了解Java,所以我正在用Java测试事物,然后将它们重写为Clojure。 I'm using Eclipse Luna with the Counterclockwise plugin, and am not using Maven, just Leiningen. 我正在将Eclipse Luna与Counterclockwise插件一起使用,而不是使用Maven,仅是Leiningen。 As a quick aside, I don't want to use Emacs because I want to focus on learning one tool at a time. 顺便说一句,我不想​​使用Emacs,因为我想一次专注于学习一种工具。

To incorporate jMusic, I've downloaded the file and am right-clicking on the Project folder, selecting Build Path, and selecting Add External Archive. 为了合并jMusic,我下载了文件,然后右键单击Project文件夹,选择Build Path,然后选择Add External Archive。 I've done that with both the Clojure project and the Java project. 我已经在Clojure项目和Java项目中做到了这一点。 I have the following Java code: 我有以下Java代码:

import jm.music.data.Note;
import jm.util.Play;

public final class TestMusic {
    public static void main(String[] args) {
        Play.midi(new Note());
    }
}

This program runs and makes noise without issue. 该程序运行并发出噪音而没有问题。 I cannot reproduce this in Clojure. 我无法在Clojure中重现此内容。 I wrote the following code (which could be wrong, but that's a separate issue): 我编写了以下代码(这可能是错误的,但这是一个单独的问题):

(ns my-clojure-test.core
  (:use jm.music.data Note)
  (:use jm.util Play))

(. Play midi (. Note))

And I get the following error: 我得到以下错误:

;; Clojure 1.6.0
CompilerException java.io.FileNotFoundException: Could not locate jm/music/data__init.class 
or jm/music/data.clj on classpath: , compiling:(my_clojure_test/core.clj:1:1) 
#<Namespace my-clojure-test.core>

I've tried doing the same right-click on the project and adding an external archive. 我尝试在项目上单击鼠标右键并添加一个外部存档。 I've moved the jMusic.jar to src/java, and also extracted its files alongside the .jar. 我已将jMusic.jar移至src / java,并在.jar旁边提取了其文件。

My project.clj file looks like this: 我的project.clj文件如下所示:

(defproject my-clojure-test "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [org.clojure/core.logic "0.8.10"]]
  :source-paths ["src" "src/my-clojure-test"]
  :java-source-paths ["src/java"])

How do I get Clojure to recognize the jMusic so I can make the aural "hello world" program play? 如何让Clojure识别jMusic,以便可以播放听觉“ hello world”程序?

EDIT: 编辑:

The selected answer does work, but do check the comments for more details about what I had to do to get it working, in case anyone else runs into the same problem. 选定的答案确实有效,但是请检查注释以获取有关我必须做的更多详细信息,以防万一其他人遇到相同的问题。

To break down the error message: 要分解错误消息:

CompilerException java.io.FileNotFoundException: Could not locate jm/music/data__init.class 
or jm/music/data.clj on classpath: , compiling:(my_clojure_test/core.clj:1:1) 
#<Namespace my-clojure-test.core>

This is saying that it was looking for either jm/music/data__init.class or jm/music/data.clj . 这就是说它正在寻找jm/music/data__init.classjm/music/data.clj Either one would suffice in order to load the namespace jm.music.data which you have asked for in your ns declaration. 为了加载您在ns声明中要求的名称空间jm.music.data ,任何一种都足够。 Of course there is no such Clojure namespace. 当然,没有这样的Clojure名称空间。 This happened because you tried to use use to access classes, and it is designed for accessing namespaces. 发生这种情况是因为您试图使用use来访问类,并且它是专为访问名称空间而设计的。 import is for accessing classes and packages. import用于访问类和包。

(ns my-clojure-test.core
  (:import (jm.music.data Note)
           (jm.util Play)))

The . . notation takes a method first, then the class, except when accessing a static method, in which case one should use / . 表示法首先使用一个方法,然后使用该类,除非访问静态方法,在这种情况下,应使用/ The proper way to invoke a constructor is with the . 调用构造函数的正确方法是使用. following the classname, without any separator. 在类名后面,没有任何分隔符。

(Play/midi (Note.))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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