NameError: cannot link Java class

hi group,
who can help me with the following error message:

I have made a class that uses the saxon classes. For the saxon classes,
I
have two seperate jarfiles; my own class resides in a .class-file
(Myclass.class). The constructor of my own class throws a
net.sf.saxon.s9api.SaxonApiException.
But I can’t get it to work, I keep getting a ‘cannot link Java class
Myclass’ message

Z jirb
irb(main):004:0> require ‘java’
=> true
irb(main):005:0> $CLASSPATH <<
‘/usr/local/oxygen_10.3/lib/saxon9-s9api.jar’
=> [“file:/usr/local/oxygen_10.3/lib/saxon9-s9api.jar”]
irb(main):006:0> $CLASSPATH << ‘/usr/local/oxygen_10.3/lib/saxon9sa.jar’
=> [“file:/usr/local/oxygen_10.3/lib/saxon9-s9api.jar”,
“file:/usr/local/oxygen_10.3/lib/saxon9sa.jar”]
irb(main):007:0> $CLASSPATH << ‘.’
=> [“file:/usr/local/oxygen_10.3/lib/saxon9-s9api.jar”,
“file:/usr/local/oxygen_10.3/lib/saxon9sa.jar”,
“file:/autofs/srv009bh/home/rdgon/project/jaarverslagen/test/./”]
irb(main):008:0> import “net.sf.saxon.s9api.SaxonApiException”
=> Java::NetSfSaxonS9api::SaxonApiException
irb(main):009:0> import “Myclass”
NameError: cannot link Java class Myclass, probable missing dependency:
net/sf/saxon/s9api/SaxonApiException
from (irb):9
irb(main):010:0>

I found a post on this list mentioning the same message, however the
solution did not apply to my situation.

Any help appreciated. Thanks in advance! Ruud


View this message in context:
http://old.nabble.com/NameError%3A-cannot-link-Java-class-tp30076702p30076702.html
Sent from the JRuby - User mailing list archive at Nabble.com.

ruud144 wrote:

I have made a class that uses the saxon classes. For the saxon classes, I
have two seperate jarfiles; my own class resides in a .class-file
(Myclass.class). The constructor of my own class throws a
net.sf.saxon.s9api.SaxonApiException.
But I can’t get it to work, I keep getting a ‘cannot link Java class
Myclass’ message

I have some more info:

jruby 1.5.1 (ruby 1.8.7 patchlevel 249) (2010-07-30 6586) (OpenJDK
Client VM
1.6.0_18) [i386-java]

The Myclass.class file is in the directory from which jirb is run (’.’).
I
found out that when I extract the saxon9sa.jar in that same directory,
the
message dissapears.

I really hope someone knows a better solution to this issue…

Ruud


View this message in context:
http://old.nabble.com/NameError%3A-cannot-link-Java-class-tp30076702p30077340.html
Sent from the JRuby - User mailing list archive at Nabble.com.

ruud144 wrote:

I have made a class that uses the saxon classes. For the saxon classes, I
have two seperate jarfiles; my own class resides in a .class-file
(Myclass.class). The constructor of my own class throws a
net.sf.saxon.s9api.SaxonApiException.
But I can’t get it to work, I keep getting a ‘cannot link Java class
Myclass’ message

The java file:

$ cat Myclass.java
import net.sf.saxon.s9api.SaxonApiException;

public class Myclass
{
public Myclass (String xslfile) throws SaxonApiException
{
throw new SaxonApiException(“test”);
}
}
$ jirb
irb(main):001:0> require ‘java’
=> true
irb(main):002:0> require ‘/usr/local/saxonb9-1-0-7j/saxon9-s9api.jar’
=> true
irb(main):003:0> require ‘//usr/local/saxonb9-1-0-7j/saxon9.jar’
=> true
irb(main):004:0> import “net.sf.saxon.trans.XPathException”
=> Java::NetSfSaxonTrans::XPathException
irb(main):005:0> import “net.sf.saxon.s9api.SaxonApiException”
=> Java::NetSfSaxonS9api::SaxonApiException
irb(main):006:0> import “Myclass”
NameError: cannot link Java class Myclass, probable missing dependency:
net/sf/saxon/s9api/SaxonApiException
from (irb):6


View this message in context:
http://old.nabble.com/NameError%3A-cannot-link-Java-class-tp30076702p30104684.html
Sent from the JRuby - User mailing list archive at Nabble.com.

ruud144 wrote in post #958480:

ruud144 wrote:

I have made a class that uses the saxon classes. For the saxon classes, I
have two seperate jarfiles; my own class resides in a .class-file
(Myclass.class). The constructor of my own class throws a
net.sf.saxon.s9api.SaxonApiException.
But I can’t get it to work, I keep getting a ‘cannot link Java class
Myclass’ message

The java file:

$ cat Myclass.java
import net.sf.saxon.s9api.SaxonApiException;

public class Myclass
{
public Myclass (String xslfile) throws SaxonApiException
{
throw new SaxonApiException(“test”);
}
}
$ jirb
irb(main):001:0> require ‘java’
=> true
irb(main):002:0> require ‘/usr/local/saxonb9-1-0-7j/saxon9-s9api.jar’
=> true
irb(main):003:0> require ‘//usr/local/saxonb9-1-0-7j/saxon9.jar’
=> true
irb(main):004:0> import “net.sf.saxon.trans.XPathException”
=> Java::NetSfSaxonTrans::XPathException
irb(main):005:0> import “net.sf.saxon.s9api.SaxonApiException”
=> Java::NetSfSaxonS9api::SaxonApiException
irb(main):006:0> import “Myclass”
NameError: cannot link Java class Myclass, probable missing dependency:
net/sf/saxon/s9api/SaxonApiException
from (irb):6


View this message in context:

http://old.nabble.com/NameError%3A-cannot-link-Java-class-tp30076702p30104684.html

Sent from the JRuby - User mailing list archive at Nabble.com.

The easiest solution is to add your ‘saxon…jar’ files to the CLASSPATH
before running the jruby part.

I managed to duplicate the issue using:

//MyClass.java
import a.MyException;

public class MyClass {
public MyClass () throws MyException {
throw new MyException();
}
}

// a/MyException.java
package a;

public class MyException extends Exception {
}

ruby.rb

require ‘java’
import “MyClass”
import “a.MyException”

begin
a = MyClass.new
rescue
puts “Caught exception”
end

With a/MyException.class in the current directory, the program works.

Placing a/MyException.class in a jar file (jar cvf mine.jar
a/MyException.class) and moving the jar file, MyClass.class and ruby.rb
to a different directory gives the NameError problem you found.

Adding ‘mine.jar’ to the CLASSPATH before running the ruby program gets
it to work.

I couldn’t find a way to get jruby to load ‘mine.jar’ and then “MyClass”
so that “MyClass” was aware of the contents of ‘mine.jar’ (and so didn’t
throw a NameError). Is it possible?

Peter.

Sohan J. wrote:

Myclass’ message


I couldn’t find a way to get jruby to load ‘mine.jar’ and then “MyClass”
so that “MyClass” was aware of the contents of ‘mine.jar’ (and so didn’t
throw a NameError). Is it possible?

Hi Peter,
thanks for the research. I am not sure if your CLASSPATH solution will
work
for me… I run my ruby script using jruby, it is not embedded in java
(or
am I missing something?).

Since I didn’t get your CLASSPATH suggestion to work, I have rewritten
the
Myclass class in jruby and that works very well too.

But I am more than interested to know what is going wrong; I have this
feeling that my understanding of jruby isn’t complete concerning this
subject…

regards, Ruud


View this message in context:
http://old.nabble.com/NameError%3A-cannot-link-Java-class-tp30076702p30120513.html
Sent from the JRuby - User mailing list archive at Nabble.com.

Benny Wanderer wrote:

What did you type to try the CLASSPATH solution?

The line: CLASSPATH=$CLASSPATH:mine.jar adds the jar file to the current
value of CLASSPATH. This is then accessible to MyClass when you use it
in jruby.

I hope that helps.

Hi Peter,
you are right, setting CLASSPATH does work indeed. I made make
conclusion
too early. I did this:

$ export
CLASSPATH=/usr/local/saxonb9-1-0-7j/saxon9-s9api.jar:/usr/local/saxonb9-1-0-7j/saxon9.jar
[email protected] jirb
irb(main):001:0> require ‘java’
=> true
irb(main):002:0> import “net.sf.saxon.s9api.SaxonApiException”
=> Java::NetSfSaxonS9api::SaxonApiException
irb(main):003:0> import “Myclass”
NameError: cannot load Java class Myclass
from (irb):3
irb(main):004:0> $CLASSPATH << ‘.’
=> [“file:/autofs/srv009bh/home/rdgon/project/jaarverslagen/test/./”]

My conclusion was: the CLASSPATH from the shell is not used. But if I
only
had tried it out:

irb(main):005:0> import “Myclass”
=> Java::Default::Myclass
irb(main):006:0>

, I would have seen that it works indeed. Thanks for the hint.
But although this is a good workaround, I find it not acceptable for an
end
solution. I am still looking for something that makes the script self
supporting.

I am not shore if this is a jruby bug and I should submit a bug report
or
not. I hope someone can advice me on this.

regards, Ruud


View this message in context:
http://old.nabble.com/NameError%3A-cannot-link-Java-class-tp30076702p30130367.html
Sent from the JRuby - User mailing list archive at Nabble.com.

Hi Ruud,

What did you type to try the CLASSPATH solution? You seem to be on
Linux. I did the following in a bash terminal (using my example from
above):

ls
mine.jar MyClass.class paths.rb
jruby paths.rb
paths.rb:2: cannot link Java class MyClass, probable missing dependency:
a/MyException (NameError)
CLASSPATH=$CLASSPATH:mine.jar
jruby paths.rb
Caught Exception

The line: CLASSPATH=$CLASSPATH:mine.jar adds the jar file to the current
value of CLASSPATH. This is then accessible to MyClass when you use it
in jruby.

You should be able to do the same, using:

CLASSPATH=$CLASSPATH:/usr/local/saxonb9-1-0-7j/saxon9-s9api.jar:/usr/local/saxonb9-1-0-7j/saxon9.jar

before running your program using jruby.

I hope that helps.

Peter.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs