I have this Java class:
public class SortAlgorithms {
public static void main(String... args) {
String out = "";
int vec1[] = readFile(args[0]);
out = out + write(vec1);
out = out + "=== INSERTION SORT ===\n";
insertionSort(vec1, vec1.length);
out = out + write(vec1) + "\n";
...
}
...
}
And I deploy it on other machine at my Boinc platform. There is a bash script to run this class:
#!/bin/sh
export JAVA_HOME="/usr/java/jdk1.6.0_34/"
export PATH=${JAVA_HOME}/bin:${PATH}
java SortAlgorithms 10 "output.txt" > saida.txt
And I receive this error:
<stderr_txt>
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1014198118 in class file SortAlgorithms
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: SortAlgorithms. Program will exit.
</stderr_txt>
Does anyone know what is happening?
Thnaks in advance.
Felipe
Your class file was not copied properly. It starts with string '<sof' which is definitely not a java bytecode start.
Looking at the Java API:
file is malformed or otherwise cannot be interpreted as a class file.
I believe that you need to compile your Java file on the machine which will be running the code.
While both maxkar and user1773630's answers seem right, it does sound like a .class file wasn't transferred properly. Compare checksums, or make a .jar?
If you've got jdk's with differing major versions installed, there can be class compatibility issues as well, which can cause a similar error.
Related
This question already has answers here:
Unsupported major.minor version 51.0 error after moving from IntelliJ IDEA JBoss to MyEclipse Tomcat
(3 answers)
Closed 7 years ago.
This is my simple Scala-Spark object created in IntelliJ IDE using SBT project (part of the code was taken from here).
package test
import org.apache.spark.mllib.tree.RandomForest
import org.apache.spark.mllib.tree.model.RandomForestModel
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
object Test {
val conf = new SparkConf()
.setMaster("local[2]")
.setAppName("CountingSheep")
val sc = new SparkContext(conf)
def main(args: Array[String]) {
// Load and parse the data file.
val data = MLUtils.loadLibSVMFile(sc, "data/adalone.txt")
// Split the data into training and test sets (30% held out for testing)
val splits = data.randomSplit(Array(0.7, 0.3))
val (trainingData, testData) = (splits(0), splits(1))
// Train a RandomForest model.
// Empty categoricalFeaturesInfo indicates all features are continuous.
val numClasses = 2
val categoricalFeaturesInfo = Map[Int, Int]()
val numTrees = 3 // Use more in practice.
val featureSubsetStrategy = "auto" // Let the algorithm choose.
val impurity = "variance"
val maxDepth = 4
val maxBins = 32
val model = RandomForest.trainRegressor(trainingData, categoricalFeaturesInfo,
numTrees, featureSubsetStrategy, impurity, maxDepth, maxBins)
// Evaluate model on test instances and compute test error
val labelsAndPredictions = testData.map { point =>
val prediction = model.predict(point.features)
(point.label, prediction)
}
val testMSE = labelsAndPredictions.map{ case(v, p) => math.pow((v - p), 2)}.mean()
println("Test Mean Squared Error = " + testMSE)
println("Learned regression forest model:\n" + model.toDebugString)
// Save and load model
model.save(sc, "myModelPath")
val sameModel = RandomForestModel.load(sc, "myModelPath")
}
}
The project was successfully compiled, however when I run it the following error appears (see below). It says Unable to load native-hadoop library for your platform.
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
15/11/25 12:04:44 INFO SparkContext: Running Spark version 1.5.1
15/11/25 12:04:49 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/spark/network/shuffle/ShuffleClient : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.apache.spark.SparkContext.createSparkEnv(SparkContext.scala:277)
at org.apache.spark.SparkContext.<init>(SparkContext.scala:450)
at test.Test$.<init>(Test.scala:13)
at test.Test$.<clinit>(Test.scala)
at test.Test.main(Test.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code 1
UPDATE:
The problem was solved after adding the following line of code inside the main loop of my Scala object:
System.setProperty("hadoop.home.dir", "D:\\SOFTWARE\\BIGDATA\\hadoop-common-2.2.0-bin-master\\")
I am working along with the ATM Case Study from Deitel java how to program 9th edition.
The case study is at chapter 13, page 546(in case someone has the book and would like to check),I am sure my code is 100% as the book suggested.
I have all the code set but when I try to run the program it is giving me this:
Error: Could not find or load main class come.example.atm.AtmRun
when I tried to compile the class by using terminal from the class path it gave me this error:
localhost:atm user$ javac AtmRun.java
AtmRun.java:5: error: cannot find symbol
Atm theATM = new Atm();
^
symbol: class Atm
location: class AtmRun
AtmRun.java:5: error: cannot find symbol
Atm theATM = new Atm();
^
symbol: class Atm
location: class AtmRun
2 errors
this is the class am running: straight forward but I cant seem to find the problem. any help?
package come.example.atm;
public class AtmRun {
public static void main (String[] args){
Atm theATM = new Atm();
theATM.run();
}
}
UPDATE: when i run the .class file from the bin directory of project using command java AtmRun i get this:
Exception in thread "main" java.lang.NoClassDefFoundError: AtmRun (wrong name: come/example/atm/AtmRun)
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)
Class Atm has a constructor Atm() and public void run() along with other methods, the class is big so i think its better if I don't post the code it however you can check in the book if you can.
Note: I am using eclipse, other projects and classes work and run properly.
for me it worked. Please follow below steps:
Y:\HashmiAb\Desktop\Trash\test>javac come\example\atm\Atm.java
Y:\HashmiAb\Desktop\Trash\test>javac come\example\atm\AtmRun.java
Y:\HashmiAb\Desktop\Trash\test>java come.example.atm.AtmRun
Heloo
It matters how you use -d and -cp options of javac and java commands. I didn't use any of this options.
For more help please find the directory structure.
+test
-+come
-+example
-+atm
-AtmRun.java
-Atm.java
Thanks.
I have wriitten following code using the Jena Library to print the URIs on a web page but it is showing a error. The code is
public static void test(String url)
{
try
{
System.out.println("to go");
Model read = ModelFactory.createDefaultModel().read(url);
System.out.println("to go");
StmtIterator si=read.listStatements();
System.out.println("to go");
while(si.hasNext())
{
Statement s=si.nextStatement();
Resource r=s.getSubject();
Property p=s.getPredicate();
RDFNode o=s.getObject();
System.out.println(r.getURI());
System.out.println(p.getURI());
System.out.println(o.asResource().getURI());
}
}
catch(JenaException | NoSuchElementException c)
{ }
}
Can anyone help me further with this problem???
It is not able to create a model read object. The URL is a web address of a xml page.
the following error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.hp.hpl.jena.util.Metadata.<clinit>(Metadata.java:39)
at com.hp.hpl.jena.JenaRuntime.<clinit>(JenaRuntime.java:37)
at com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.<clinit>(RDFReaderFImpl.java:74)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.<clinit>(ModelCom.java:54)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:142)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:136)
at web.crawler.WebCrawler.test(WebCrawler.java:52)
at web.crawler.WebCrawler.main(WebCrawler.java:98)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
Add all the jars in the lib/ directory of the distribution. You can use the lib/* form for the classpath.
if you then still get missing org.w3c.dom.ElementTraversal (which is in xml-apis), it's because you have an older version of Xerces and old xml-apis.
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
the library slf4j*.jar is missing from your classpath.
Following are the libraries I had to add more:
import org.slf4j.*;
import org.apache.xerces.util.XMLChar;
import org.w3c.dom.ElementTraversal;
import org.apache.jena.iri.IRIFactory;
I solved it adding all the libraries availables at jena lib dir.
I gess Jena need another lib or jar file, but I dont know the especific .jar, so I add all of them.
Then it works!
I have built my own custom AWT classes in my home folder in java_src/classes.
Each of the java files contain the package classes; declaration at the top.
I also created a sample program called ScreenDemo.java and placed it in the java_src/ folder to use the custom AWT classes instead of java.awt.
//ScreenDemo.java
import classes.Screen;
class ScreenDemo
{
public static void main(String args[])
{
Screen.init(20,15,3);
}
}
But when i attempt to compile ScreenDemo.java,an error is displayed
java_src/ScreenDemo.java:1: package classes does not exist
import classes.Screen;
^
java_src/ScreenDemo.java:6: cannot find symbol
symbol : variable Screen
location: class ScreenDemo
Screen.init(20,15,3);
^
2 errors
When i add the path i encounter this error
Exception in thread "main" java.lang.NoClassDefFoundError: ScreenSample (wrong name: classes/ScreenSample)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: ScreenSample. Program will exit.
While compiling the code the compiler is complaining about the missing class Screen.java which i assume is inside "classes" package.
Either you need to add src\classes\ to the src folder list or move the Screen.java to the same location as ScreenDemo.java and then try to compile again.
I've just started using Spock to write tests for my Java project. First I had some trouble figuring out how to compare floats with an accepted diff (like the delta parameter in JUnit's assertEquals).
I haven't found any other way to do this than either using the methods in JUnit's Assert class, except using the closeTo matcher that appears to be bundled with Spock. Still, I'm unable to make it work.
If I try making a test with closeTo, I end up with the following error.
Am I missing a dependency, doing it wrong, or what?
import static spock.util.matcher.HamcrestMatchers.closeTo
import spock.lang.Specification
class MatcherTest extends Specification {
def "test closeTo"() {
expect:
1.9d closeTo(2, 0.5)
}
}
java.lang.NoClassDefFoundError: org/hamcrest/TypeSafeMatcher
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at spock.util.matcher.HamcrestMatchers.closeTo(HamcrestMatchers.groovy:47)
You are missing org.hamcrest:hamcrest-core:1.2, which is referenced from the spock-core POM.