MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: JLink : JavaNew Failure

  • To: mathgroup at
  • Subject: [mg66383] Re: [mg66250] JLink : JavaNew Failure
  • From: caa0012 at
  • Date: Thu, 11 May 2006 02:15:24 -0400 (EDT)
  • References: <>
  • Sender: owner-wri-mathgroup at


Just a few pointers...
1.  You don't need to call LoadJavaClass unless you just want to use 
some static methods without instantiating a class.
2.  Use a package hierarchy.  For example, in your java source put:

package mypackage;

3.  Then when you want an instance of test, you do JavaNew["mypackage.test"]

For this to work successfully, your directory structure is critical.  
Suppose all your java code is in home/myclasses.  For this to work, 
you'll actually need a subdirectory home/myclasses/mypackage where the 
test .class and .java files should be placed.  In other words, the 
package structure needs to mimic exactly the directory structure, and 
then it is only necessary to AddToClassPath the top directory.

If your class truly has a no-arg constructor then 
JavaNew["mypackage.test"] should work.  If you have a constructor that 
takes a double then JavaNew["mypackage.test",1] might give you an error 
because mathematica casts it to an integer type.  Instead you could do 

One more thing...if you're not using the JVM bundled with mathematica, 
then you're going to need to be sure that your system classpath 
variable is pointing to the java library files otherwise you'll get 
errors that way too.

Hope that helps...


Quoting karenechu at

> Hello, everyone,
> I am very new to J/Link but I  have been fiddling with it for a while
> but I just don't see what was wrong:
> I wrote a hello world program in Java and compiled it and put it in a
> folder. Then I tried to call it from Mathematica using:
> Needs["JLink`"];
> InstallJava[CommandLine->"home/usr/java/bin/java"];
> AddToClassPath["home/myclasses"];
> LoadJavaClass["test"];
> All these go well with the new class path added and
> JavaClass["test",9] as output to the last line, but when:
> test1= JavaNew["test"];
> Mathematica failed to created object because " there is no constructor
> that takes zero arguments"   even though I put in an explicit
> constructor that takes no argument.
> I tried putting in a constructor taking an int as an argument and using
> test1= JavaNew["test",1]; (after reinstalling Java etc)
> but now the type of the argument is wrong.
> I think the class is either not properly loaded or something, but I
> just dont' know what is wrong!!
> Please help!
> Thanks a lot,
> cheers,
> Kay

  • Prev by Date: Re: Problem calling function in DLL via NETLink: NET::netexcptn: A .NET exception occurred
  • Next by Date: Beginner--How to simulate multiple returns for a function?
  • Previous by thread: JLink : JavaNew Failure
  • Next by thread: Re: JLink : JavaNew Failure