I have no experience with Scala, so this question may be elementary. I am trying to use Scala class from within Java, based on the "Person" example in this tutorial: http://www.codecommit.com/blog/java/interop-between-java-and-scala
I create two source files, one Scala and one Java, as follows.
Person.scala:
class Person {
def getName() = "Daniel Spiewak"
}
Test.java:
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.getName();
}
}
I can compile (with a warning I don't understand), but I get a ClassNotFoundException when I try to run the program.
$ scalac Person.scala
$ javac Test.java
./Person.class: warning: Cannot find annotation method 'bytes()' in type 'ScalaSignature': class file for scala.reflect.ScalaSignature not found
1 warning
$ java Test
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at Test.main(Test.java:3)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 13 more
Any idea what I'm doing wrong?
You need to include the Scala runtime library to be able to use Scala classes. This is the cause of both the compiler warning and the error at runtime.
Try this
java -cp .:scala-library.jar Test
(the name of the jar file might be different, but the point is to add it to your classpath).
Did you put scala-library.jar in your classpath? Seems you forgot this, so JVM cannot find some internal class for Scala runtime.
In your link http://www.codecommit.com/blog/java/interop-between-java-and-scala:
Just stick scala-library.jar on your classpath and all of your Scala
classes should be readily available within your Java application.
Related
I have a source file named Plane.java. I am trying to compile and run the program in terminal, but i can't!
I am compiling it with:
javac Plane.java
So, I am getting with ls my classes
CargoBay.class
CleaningEmployee.class
Employee.class
EquipmentComponent.class
MaintenanceEmployee.class
PassengerComponent.class
Plane.class
Plane.java
PlaneComponent.class
PrivateComponent.class
SecurityEmployee.class
Now, I have no idea how to run it! I am trying with java Plane, but I keep getting errors. Any ideas ?
(errors: after java Plane)
Exception in thread "main" java.lang.NoClassDefFoundError: Plane (wrong name: plane/Plane)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The program runs just fine in netbeans
Compile your class by
javac -d . Plane.java
This will create a plane directory at current location and put your classes into that directory then run your program using
java plane.Plane
Your classes are probably in a package named plane. The compiler creates a directory with the package name. What you have to do is go up one level from there
and call:
java plane.Plane
Other options would be to set the CLASSPATH environment variable or use the -cp option of the java command.
I tried to execute CreateTextFileTest.class file with classpath in the terminal as the following:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile" CreateTextFileTest
My class files in this directory : /home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile .
I have 3 classes in the directory: AccountRecord.class , CreateTextFile.class, and CreateTextFileTest.class
But, when I executed the CreateTextFileTest from the terminal with the code above, an error occured like that:
Exception in thread "main" java.lang.NoClassDefFoundError: CreateTextFileTest (**wrong name**: unit17/creatingtextfile/CreateTextFileTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
As far as I understand because CreateTextFileTest.java uses two different classes, executing fails. How to handle this situation? How to run my java project? How to use classpath in this situation? Am I using wrongly?
The error message states that the package name declared in your compiled class doesn't match what the JVM was expecting given your classpath setup. The classpath should point to the directory from which your packages start, not the directory actually containing your .class files. Given your error message I believe that this should work:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17" unit17.creatingtextfile.CreateTextFileTest
Given the duplicate unit17 in your path I get the impression there's something else mixed up here. You'll get a better answer on Stack Overflow...
I am using Eclipse with Eclipse Maven Plugin (m2e).
My java program compiles and run correctly from eclipse interface but I am unable to compile and run it from terminal.
My Eclipse Setting:
I am using two third party APIs, for which in eclipse build path I added
"/home/syed/workspace/FirstMaven/target/resources/fuse-jna-master/build/classes" (as external class folder)
"/home/syed/workspace/FirstMaven/target/resources/apache-jena-2.11.1/lib" (as external jars)
Package:
package org.organization.upesh.FirstMaven;
My Project path:
syed#ubuntu:~/workspace/FirstMaven$
Source Code Directory Path:
syed#ubuntu:~/workspace/FirstMaven/src/main/java/org/organization/upesh/FirstMaven$
Classes Directory:
syed#ubuntu:~/workspace/FirstMaven/target/classes/org/organization/upesh/FirstMaven$
When I try to execute myProgram via below command
syed#ubuntu:~/workspace/FirstMaven/target/classes$ java org.organization.upesh.FirstMaven.myProgram
it gives me these errors:
Exception in thread "main" java.lang.NoClassDefFoundError: net/fusejna/util/FuseFilesystemAdapterFull
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Caused by: java.lang.ClassNotFoundException: net.fusejna.util.FuseFilesystemAdapterFull
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
But my test program that do not use the third party API runs correctly via:
syed#ubuntu:~/workspace/FirstMaven/target/classes$ java org.organization.upesh.FirstMaven.test
I think myProgram is not executing because of the two APIs (class folder and jar folder) that I am using.
I have added path of class and jar folders of the APIs to /etc/environment (given below) and rested my computer, but still the same error
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/syed/workspace/FirstMaven/target/resources/apache-jena-2.11.1/lib:/home/syed/workspace/FirstMaven/target/resources/fuse-jna-master/build/classes"
Please guide me how to run my program correctly
JVM does not get libraries from PATH. It uses special environment variable CLASSPATH that can contain a list of directories or jar files separated by colon on Unix or semicolon on Windows.
So, just define CLASSPATH and put references to all your libraries there.
Aleternatively (and IMHO better) use command line switch -classpath (or its alias -cp) when running java:
java -cp mylib1.jar:mylib2.jar com.mycompany.Main
Reproducing the instructions at
http://stuf.ro/calling-c-code-from-java-using-jna
for calling C code from java I have:
/* ctest.c */
#include <stdio.h>
void helloFromC()
{
printf("Hello from C!\n");
}
This is compiled into the library using:
gcc -o libctest.so -shared ctest.c
Then Java calling program is:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class HelloWorld
{
public interface CTest extends Library
{
public void helloFromC();
}
public static void main(String argv[])
{
CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
ctest.helloFromC();
}
}
Then the java is compiled in Java8 using the -cp classpath argument:
javac HelloWorld.java -cp jna.jar
This step works, producing two class files, HelloWorld.class and HelloWorld$1.class.
The execute step does not work:
java HelloWorld -cp jna.jar
instead producing the following output:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Library
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:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at HelloWorld.main(HelloWorld.java:14)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
The output is effectively identical to: JNA example program java.lang.NoClassDefFoundError which I have carefully reviewed.
I have run many tests of java8 under cygwin and they all work, this one does not.
I have constructed many permutations of this example, including creating the sun.com.jna directories and including HelloWorld in the package. I have also used different delimiters which are necessary in some circumstances. All of them fail. What am I missing?
It seems that you didn't set your classpath. NoClassDefFoundErrorindicates that your ClassLoadercan't find your compiled classes.
This question already has answers here:
Building JAR that includes all its dependencies
(5 answers)
Closed 8 years ago.
I developed a Java project in which I wrote some Scala classes and some Java classes. I used Scala-Neo4j wrapper in the project to write Scala functions for inserting and retrieval of Neo4j nodes. I also wrote Thrift services in java in the project as I need to call these Scala functions from Php. Finally I extracted a jar file using Eclipse and I tried to run it from the command line and I got the following Exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/neo4j/scala/Neo4jWrapper
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at dum.Sad.main(Sad.scala)
at dum.ScalaRunner.main(ScalaRunner.java:7)
Caused by: java.lang.ClassNotFoundException: org.neo4j.scala.Neo4jWrapper
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 14 more
It works completely fine when I run the project from Eclipse. But, while running the jar file which I extracted from the project I get the Exception above. Where did i go wrong?
It seems your are not setting the classpath up properly. Try this:
scala -cp ./where/is/Neo4jWrapper/jar YourClass
Of course, replace ./where/is/Neo4jWrapper/jar with the path to the .jar. Notice that if you need to point to more than one jar or class directory you need to separate them with : if on Linux/Mac or ; if in Windows. And avoid spaces in the classpath.