I've decided I want to use Clojure's Java interoperability to play with the jMusic library. 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. I'm using Eclipse Luna with the Counterclockwise plugin, and am not using Maven, just Leiningen. As a quick aside, I don't want to use Emacs because I want to focus on learning one tool at a time.
To incorporate jMusic, I've downloaded the file and am right-clicking on the Project folder, selecting Build Path, and selecting Add External Archive. I've done that with both the Clojure project and the Java project. I have the following Java code:
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. 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.
My project.clj file looks like this:
(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?
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. Either one would suffice in order to load the namespace jm.music.data which you have asked for in your ns declaration. Of course there is no such Clojure namespace. This happened because you tried to use use to access classes, and it is designed for accessing namespaces. import is for accessing classes and packages.
(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.))
Related
I'm currently working on extending racklog, which is a library I installed by running raco pkg install in the repo directory.
I'm trying to provide a new function, namely a struct constructor. Currently, I'm defining the struct as follows in racklog.rkt. I then provide it from that file.
; racklog.rkt
(struct my-struct (value))
(provide my-struct)
The main file of the library just provides everything from this file:
; main.rkt
(require "racklog.rkt")
(provide (all-from-out "racklog.rkt"))
However, when I try to use the provided constructor (which should be named my-struct) in a file requiring this module, it says that the id isn't found. In particular, I'm trying:
; test.rkt
(require racklog)
my-struct
This also happens even with non-struct things such as defined variables, functions, etc. All the other provided forms seem to be working fine. What's the way I can fix this so I can use the provided constructor? Thanks!
This is an unfortunate stale compiled file issue. Try raco setup --pkgs racklog to compile racklog and run your program again. It should now work. Alternatively, you can manually delete the compiled directories.
In emacs cider repl, now I know how to use clojure.tools.namespace in a leiningen project. However, when I use it on a single clj file which doesn't belong to any project, it seems clojure.tools.namespace doesn't work on the file:
=> #<FileNotFoundException java.io.FileNotFoundException: Could not locate com/foo__init.class or com/foo.clj on classpath: >
I have declared clojure.tools.namespace in .lein/profile.clj and require it in the clj file. How should I make clojure.tools.namespace work on a single clj file?
My profile.clj
{:user
{:repl-options {:timeout 128000}
:plugins [;; REPL
[cider/cider-nrepl "0.9.0-SNAPSHOT"]
[refactor-nrepl "0.2.2"]
;; Application server
[lein-immutant "2.0.0-SNAPSHOT"]
;; Automated testing
[lein-cloverage "1.0.2"]
[lein-test-out "0.3.1"]
;; Package management
[lein-ancient "0.6.2"]
[lein-clojars "0.9.1"]
;; Documentation
[codox "0.6.8"]
[lein-clojuredocs "1.0.2"]
;; Static analysis
[lein-typed "0.3.4"]
;; [jonase/eastwood "0.1.2"]
[lein-bikeshed "0.1.6"]
[lein-kibit "0.0.8"]]
:jvm-opts ["-Dapple.awt.UIElement=true"]
:dependencies [[org.clojars.gjahad/debug-repl "0.3.3"]
[difform "1.1.2"]
[spyscope "0.1.4"]
[org.clojure/tools.trace "0.7.8"]
[org.clojure/tools.namespace "0.2.9"]
[im.chit/vinyasa "0.2.0"]
[slamhound "1.5.5"]
[criterium "0.4.3"]]
:injections [(require 'spyscope.core)
(require 'alex-and-georges.debug-repl)
(require 'com.georgejahad.difform)
(require '[vinyasa.inject :as inj])
(inj/inject 'clojure.core '>
'[[clojure.repl apropos dir doc find-doc pst source]
[clojure.tools.trace trace trace-forms trace-ns trace-vars
untrace-ns untrace-vars]
[clojure.test run-tests run-all-tests]
[clojure.pprint pprint pp]
[com.georgejahad.difform difform]
[alex-and-georges.debug-repl debug-repl]
[vinyasa.pull pull]])]}}
While this doesn't answer your specific question, it might provide some ideas on
alternative workflows which may help.
lein-exec This is a lein plugin that
lets you write clojure scripts or evaluate clojure expressions on the command line
in a similar way to what can be done with shells, python, ruby, perl etc. Of
course, startup time is a bit high, but this plugin will help you deal with
dependencies etc inside your single clj script file. (there are other possible
solutions to improve the startup speed). See this blog post for some examples
http://charsequence.blogspot.in/2012/04/scripting-clojure-with-leiningen-2.html
A scratch project. I have a fairly generic project called scratch. It just allows
me to create a new file within the src directory of the project which I can use for
experiments or demonstrations. For example, I have a file called stackoverflow.clj
within this scratch project which I use when working out the answer to a clojure
question on stack overflow. In fact, I have a lot of individual clj files in this
directory, each just representing a simple idea, test, experiment etc. It isn't
really a project i.e. I couldn't do a lein run at the root of the project and
expect anything meaningful. However, I can go to the source directory in emacs,
open a file and then run cider.
I'm just learning how to lein, and I'd like to use from a Java source
a class created by deftype in a Clojure source. This wasn't covered in the basic
tutorial and I can't get it to work properly.
The problem is that Java source can't import Clojure class, since it hasn't
been compiled yet. And Clojure class isn't compiled, since compilation is aborted
by the Java source.
I give a minimal example:
Create a new project with:
lein new app javafoo
Add to project.clj
:aot :all
:java-source-paths ["src/java"]
Put into src/javafoo/core.clj:
(ns javafoo.core)
(deftype PPoint [x y])
Put into src/java/JavaFoo.java:
package foo.java;
import javafoo.core.PPoint;
public class JavaFoo {
public static void main(String[] args) {
System.out.println("JavaFoo");
}
}
Try to compile
lein compile
It fails with package javafoo.core doesn't exist.
So now I have to
Comment out :java-source-paths
Compile
Uncomment :java-source-paths
Compile
It finally works. Is there a way to make it work from the start?
Add this line to your project.clj:
:prep-tasks [["compile" "javafoo.core"] "javac"]
I'm trying to run a simple java test code. I'm getting a "can not find or load main class file "(something like that)
The tutorial I'm following uses this command
-> javac name.java (javac doesn't work, using ->java ..)
-> dir (shows the classname as a file)
> java classname
> outputs "hello world"
I can't seem to get past the ->java running.java
class apples //everything begins with a class - need this to do anything
{
public static void main(String args[])//method
{
System.out.println("Hello World");
}
}
There could be a few problems here. Check the following:
Are you saving the file as the name of the class plus .java, e.g. apples.java
When you execute it, are you typing the name of the class or the name of the class file? you should be typing java apples, not java apples.class or java apples.java.
EDIT:
I Noticed you haven't compiled the program using javac, which makes the progrm unrunnable by java [program_name]. You need to run javac [java_sourcde_file_name] to generate a .class file. If javac doesn't work, maybe:
You don't have the JDK (Java Development Kit) installed and should download it from Oracle
javac is not in your PATH - unlikely but possible - see http://www.apl.jhu.edu/~hall/java/beginner/settingup.html.
javac runs properly but your program doesn't compile properly. this seems unlikely given the program you posted looks fine.
class Apples //- need this to do anything
{
public static void main(String args[])//everything begins with a method
{
System.out.println("Hello World");
}
}
Make sure you are in current directory of java file
compile as
javac Apples.java
Run as
java Apples
Before working in it , should need to know the coding convention it would be better to work java
Well. All you have to do in Jaca is navigate you where your class files are stored and then use java "class name". You DO NOT need to put .java or .class. Just the name.
change your class name from apples to Apples (naming convention for java class names):
http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java
also recommend you to change the file name accordingly...
then recompile and run it
javac Apples.java
java Apples
cheers
I'm just starting with Clojure and can't access to the doc function.
I'm using clojure 1.3 with emacs24 and swank-clojure.
user> *clojure-version*
{:major 1, :minor 3, :incremental 0, :qualifier nil}
But when I try:
(doc doc)
I get:
Unable to resolve symbol: doc in this context
[Thrown class java.lang.RuntimeException]
I've read Why does REPL treat clojure.core/doc as a var? and as suggested:
(clojure.repl/doc doc)
But then, I receive:
clojure.repl
[Thrown class java.lang.ClassNotFoundException]
It seems I am not "importing" the usual namespaces, but really doesn't know how to do it.
Thanks.
UPDATE
Using clojure from java (java -jar ...) it works well, so it's a problem with the emacs setup.
You need to grab the clojure.repl namespace one way or another:
From the REPL
user> (use 'clojure.repl)
user> (doc doc)
or in your program
(ns foobar
(:use [clojure.repl]))
Add the following to your Leiningen user.clj file (on Mac / Linux, it's ~/.lein/user.clj):
;; ~/.lein/user.clj
(if (>= (.compareTo (clojure-version) "1.3.0") 0)
(do (use 'clojure.repl)
(use 'clojure.java.javadoc)))
This will cause Leiningen to automatically import those two namespaces at startup for projects using Clojure 1.3.0 and later (but not for projects using Clojure 1.2.1 or earlier - where doc and source were always available).
Credit goes to Matthew Boston for this. Note also Phil Hagelberg's reply which points out most of the REPL-specific functionality is accessible directly in Emacs / Slime without needing the functions directly in the REPL.
As of Lein 2, namespaces can be automatically imported at startup using :injections, e.g:
;; ~/.lein/profiles.clj
{:user {:plugins [[lein-swank "1.4.4"]
[lein-noir "1.2.1"]
[lein-pprint "1.1.1"]]
:injections [(use 'clojure.repl)
(use 'clojure.java.javadoc)
(use 'clojure.pprint)] }}
But see other responses for SLIME equivalents.
I'm unsure of when this became the case, but as of lein 2.2 doc is available at the repl by default.