3

I have a system where I call a Java Program from Python via JPype. I'd like to switch my Java code to Scala. For example, I had:

package jnets;

class JVector{
    double [] x;
    public JVector(double[] vec){
        this.x = vec;
    }
    public double[] plus(double[] y){
        assert (x.length==y.length);
        double[] z = new double[x.length];
        for (int i=0; i<x.length; i++)
            z[i] = x[i]+y[i];
        return z;
        }
    }

After compiling this file with javac, I can call (in Python):

import jpype as jp
import os
import numpy as np
jp.startJVM('/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/MacOS/libjli.dylib')
c = jp.JClass('jnets.JVector')(np.random.randn(5))

Now, I try to do the same in Scala:

package jnets;

class SVector(xi: Array[Double]){
  var x = xi
  def plus(y: Array[Double]): Array[Double] = {
    assert(x.length==y.length)
    val z = Array[Double](x.length)
    for (i <- 0 until x.length)
      z(i) = x(i)+y(i)
    return z
  }
}

Now, if I replace the c = jp.JClass('jnets.JVector')(np.random.randn(5)) with c = jp.JClass('jnets.SVector')(np.random.randn(5)), I get an error:

jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class jnets.SVector not found

So, what's up? If I javap JVector.class I get:

class jnets.JVector {
  double[] x;
  static final boolean $assertionsDisabled;
  public jnets.JVector(double[]);
  public double[] plus(double[]);
  static {};
}

And if I do javap SVector.class, I get:

public class jnets.SVector {
  public double[] x();
  public void x_$eq(double[]);
  public double[] plus(double[]);
  public jnets.SVector();
  public jnets.SVector(double[]);
}

SO.... I'm guessing that it's something to do with this static {}; line in the Java class, but can'f figure out how to make scalac make the .class file identical to the java one.

Any ideas?

4

0 回答 0