using records in Java

92 views
Skip to first unread message

Earl J. Wagner

unread,
Mar 2, 2011, 11:38:26 PM3/2/11
to Clojure
Hi all,

I'm running into a problem with Java code using records defined in
Clojure (1.2.0) code.

Here's the Clojure code:
(ns t.core
(:gen-class))
(defrecord TRecord [a b c])
(compile 't.core)

I run Lein (1.3) to compile this and generate a jar file. Inside the
jar file I see an entry for the record class:
t/core/TRecord.class

Then I try to compile Java code to use it:

import java.lang.reflect.Method;
import t.core;
import t.core.TRecord;

class Main {
public static void main (String [] args) {
try {
Class trClass = Class.forName ("t.core.TRecord");
System.out.println ("trClass: " + trClass);
Method trMethods [] = trClass.getDeclaredMethods ();
for (Method trMethod : trMethods) {
System.out.println ("trMethod: " + trMethod);
}
} catch (Exception e) {
e.printStackTrace ();
}
}
}

Attempting to compile this with the jar in the classpath produces a
"cannot find symbol" error for the TRecord class. It does however
compile with the last import line commented out, and when run this
successfully prints out the name of the class and its methods.

I'd like to just import the TRecord class, and instantiate and use
TRecord objects. Any idea what I'm missing here?

Thanks for any help with this.

-Earl

Aaron Cohen

unread,
Mar 3, 2011, 1:48:12 PM3/3/11
to clo...@googlegroups.com
On Wed, Mar 2, 2011 at 11:38 PM, Earl J. Wagner <dont.sp...@gmail.com> wrote:

Then I try to compile Java code to use it:

import java.lang.reflect.Method;
import t.core;
import t.core.TRecord;
 
Attempting to compile this with the jar in the classpath produces a

"cannot find symbol" error for the TRecord class. It does however
compile with the last import line commented out, and when run this
successfully prints out the name of the class and its methods.

Are you sure that the "Cannot find symbol" error is referring to the line "import t.core.TRecord;"?

I'm pretty sure the line before that: "import t.core;" is invalid, you can't import a package in Java (you could do "import t.core.*" if you wanted though).

--Aaron

Daniel Solano Gomez

unread,
Mar 3, 2011, 2:24:53 PM3/3/11
to clo...@googlegroups.com

Well, t.core exists because in his ns declaration he included
:gen-class. Now, this isn't necessary for compiling records. As for
why it isn't compiling, I am not sure. I tried replicating the problem
(albiet without Leiningen), but was unable to.

Here is the code I used:

t/core.clj:

(ns t.core)

(defrecord TRecord [a b c])

(compile 't.core)

---

Main.java:

import t.core.TRecord;

public class Main {


public static void main(String[] args) {

System.out.println(new TRecord(1,2,3));
}
}

----

Sincerely,

Daniel Solano Gómez

Aaron Cohen

unread,
Mar 3, 2011, 2:47:14 PM3/3/11
to clo...@googlegroups.com
On Thu, Mar 3, 2011 at 2:24 PM, Daniel Solano Gomez <clo...@sattvik.com> wrote:
On Thu Mar  3 13:48 2011, Aaron Cohen wrote:
> On Wed, Mar 2, 2011 at 11:38 PM, Earl J. Wagner <dont.sp...@gmail.com>wrote:

Well, t.core exists because in his ns declaration he included
:gen-class.  Now, this isn't necessary for compiling records.  As for
why it isn't compiling, I am not sure.  I tried replicating the problem
(albiet without Leiningen), but was unable to.

Hmm, it's illegal to have a class name that collides with a package name, isn't it? If :gen-class creates a class named "t.core", then you won't be able to have a record named "t.core.TRecord", at least not in a way that actually works consistantly. Clojure should probably prevent this from compiling.

Earl J. Wagner

unread,
Mar 4, 2011, 4:44:21 PM3/4/11
to Clojure
Aaron, Daniel,

I appreciate your help with this. Yes, the problem was the package and
class name collision. Daniel's solution to remove the :gen-class
worked for me.

Thanks,

-Earl



On Mar 3, 2:47 pm, Aaron Cohen <aa...@assonance.org> wrote:
> On Thu, Mar 3, 2011 at 2:24 PM, Daniel Solano Gomez <cloj...@sattvik.com>wrote:
>
> > On Thu Mar  3 13:48 2011, Aaron Cohen wrote:
> > > On Wed, Mar 2, 2011 at 11:38 PM, Earl J. Wagner <
> > dont.spam.e...@gmail.com>wrote:
Reply all
Reply to author
Forward
0 new messages