0

I'm a bit new to interoping between java and scala. I want to call a companion object in Scala from Java.

File structure (in current directory):

Board.scala
Matching.java
lib/scala-library.jar
lib/scala-swing_2.11-1.0.1.jar

Matching.java

public class Matching{
  
  public void startGame(){
    new Board();
  }
  public static void main (String[] args){
    Matching newGame = new Matching();
    newGame.startGame();
  }
}

Board.scala

import scala.swing._

class Board extends MainFrame {
  title = "Matching Game"
  contents = new GridPanel(3, 2) {
    contents += new Label("Test Label")
    contents += new Button("Test Button1")
    contents += new Button("Test Button2")
    contents += new Button("Test Button3")
    contents += new CheckBox("Check1")
    contents += Button("Close") { sys.exit(0) }
  }
  visible = true;
}
object Board {
def main(args: Array[String]) {
    val ui = new Board
    ui.visible = true
    }
}

Command Line:

scalac Board.scala
javac Matching.java

java -cp /lib/*.jar:. Matching

However upon running, the terminal gives:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/swing/MainFrame

Solely doing: scalac Board.scala; scala Board works perfectly. Am I forgetting something? I hope I am linking the jars correctly. Thank you!

Edit: Stack Trace Exception

java -cp 'lib/*.jar:.' Matching
Exception in thread "main" java.lang.NoClassDefFoundError: scala/swing/MainFrame
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
 at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
 at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 at Matching.startGame(Matching.java:4)
 at Matching.main(Matching.java:8)
Caused by: java.lang.ClassNotFoundException: scala.swing.MainFrame
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 ... 14 more
make: *** [run] Error 1
LeggoMaEggo
  • 512
  • 1
  • 9
  • 24
  • Isn't MainFrame part of scala.swing._? – LeggoMaEggo Nov 19 '15 at 02:01
  • Yup: "java -cp /lib/*.jar:. Matching" should take care of both jars to my knowledge. – LeggoMaEggo Nov 19 '15 at 02:02
  • That `*` is tricky. You probably have to quote it so that the shell does not expand it. `java -cp '/lib/*j.jar:.' Matching` http://stackoverflow.com/a/13609448/14955 – Thilo Nov 19 '15 at 02:04
  • Didn't work. Tried both the single quote and double quote. May you write the example where you manually include both jars rather than use the wildcard? Thanks. – LeggoMaEggo Nov 19 '15 at 02:09
  • `/lib/` looks wrong. Should be `lib/` (not an absolute path) – Thilo Nov 19 '15 at 02:11
  • Yes, it really is. I presume scala-library.jar handles basic scala functionality and scala-swing_2.11-1.0.1.jar handles Scala swing components – LeggoMaEggo Nov 19 '15 at 02:12
  • Ah nice catch! But that still gives the same exception. Running: `java -cp 'lib/*.jar:.' Matching` with both single and double quotes. – LeggoMaEggo Nov 19 '15 at 02:14
  • 1
    Show the whole exception stack trace. Including any "caused by" if there is. – Thilo Nov 19 '15 at 02:16
  • You need to be setting the classpath for `javac` as well, not just for `java`. – Thilo Nov 19 '15 at 02:18
  • Edited post with stack trace. And new javac comment `javac -cp 'lib/*.jar:.' Matching.java` Same exception. :/ – LeggoMaEggo Nov 19 '15 at 02:22
  • That `*` is tricky. It is not `*.jar` but just `*`. I've never used it, so I am stumbling about as well. Better to explicitly list all jar files (and have a build tool to manage that). – Thilo Nov 19 '15 at 02:25
  • Got it! Thanks for treading it through with me though. See answer. :) – LeggoMaEggo Nov 19 '15 at 02:37

1 Answers1

0

Fixed it! Hope this helps!

scalac Board.scala
javac -cp lib/*:. Matching.java
java -cp lib/*:. Matching
LeggoMaEggo
  • 512
  • 1
  • 9
  • 24