Welcome! Please see the About page for a little more info on how this works.

0 votes
in Syntax and reader by
edited by

Hi all,

java pubilc class A has public proprety int a; when (def a(A.)) and then (.a a) the error A (wrong name :a) is shown.

Why is that?

[edit]

// silly example to demonstrate access patterns to A from clojure

public class A {

public int af() { return 2; }
public int af1(int x) { return x+2; }
public int a = 1;
static public int as;
static public int afs() { return 20; }
static public int afs1(int x) { return x+20; }

}

$ javac A.java
$ clojure -cp "."
user => (import A)
user => (def a (A.))
user => (def x (A.))
user => (def a1 (A.))
user => (.a a1)
1
user => (.a x)
1
user => (.a a)
Execution error (NoClassDefFoundError) at java.lang.ClassLoader/defineClass1 (ClassLoader.java:-2).
A (wrong name: a)

;; why a is not allowed?

3 Answers

+1 vote
by

If you're specifically trying to access the property then you need to use (.-a a)

0 votes
by

Can you share a more detailed example? Not sure I understand what you're seeing.

0 votes
by

I attempted to reproduce based on the code above and it failed to reproduce

% cat A.java
public class A {

public int af() { return 2; }
public int af1(int x) { return x+2; }
public int a = 1;
static public int as;
static public int afs() { return 20; }
static public int afs1(int x) { return x+20; }

}
% javac A.java
% clojure -Scp "."
Error: Could not find or load main class clojure.main
Caused by: java.lang.ClassNotFoundException: clojure.main
zsh: exit 1     clojure -Scp "."
% clojure -Sdeps '{:paths ["."]}'
Clojure 1.10.2
user=> (import A)
A
user=>  (def a (A.))
#'user/a
user=> (def x (A.))
#'user/x
user=> (def a1 (A.))
#'user/a1
user=>  (.a a1)
1
user=> (.a x)
1
user=>  (.a a)
1
user=>

It looks like your clojure script is not the tools.deps clojure script, since that has no -cp option, what script is it and what else is it doing besides launching clojure? Your repl also doesn't print the version of clojure when you start it, what repl are you using, what version of clojure?

by
Hi hiredman

thank you for feedback

I am on Win10 WSL
 
Linux 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux

openjdk 16.0.1 2021-04-20
OpenJDK Runtime Environment (build 16.0.1+9-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 16.0.1+9-Ubuntu-120.04, mixed mode, sharing)

Clojure 1.10.1
user=>

I start clojure with classpath "."

$ clojure -cp "."
Clojure 1.10.1
user=> (import A)
A
user=> (.a (A.))
1
user=> (def a (A.))
#'user/a
user=> (.a a)
Execution error (NoClassDefFoundError) at java.lang.ClassLoader/defineClass1 (ClassLoader.java:-2).
A (wrong name: a)

I guess there is some incompatibility between java toolchain and clojure on my system
by
no, it is likely something about the `clojure` command you are running that is the problem. It is not the `clojure` command that is installed from https://www.clojure.org/guides/getting_started so I am not sure what it is and what other code is running. I can tell it is not the clojure command from the above guide because the clojure command from the above guide doesn't accept a `-cp` flag.

It is possible that there is some other code running as part of your clojure command that is trying to provide extra tooling around the repl and that is what is throwing an error, however without develing into what your clojure command is actually doing we cannot know.
...