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

12
  • Isn't MainFrame part of scala.swing._? Commented Nov 19, 2015 at 2:01
  • Yup: "java -cp /lib/*.jar:. Matching" should take care of both jars to my knowledge. Commented Nov 19, 2015 at 2:02
  • That * is tricky. You probably have to quote it so that the shell does not expand it. java -cp '/lib/*j.jar:.' Matching stackoverflow.com/a/13609448/14955 Commented Nov 19, 2015 at 2: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. Commented Nov 19, 2015 at 2:09
  • 1
    Show the whole exception stack trace. Including any "caused by" if there is. Commented Nov 19, 2015 at 2:16

1 Answer 1

0

Fixed it! Hope this helps!

scalac Board.scala javac -cp lib/*:. Matching.java java -cp lib/*:. Matching 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.