What does this JSON processing error indicate? - java

I am trying to process a JSON file, but I have no idea why this exception is being triggered. I am using sample code, so I am not sure how org.codehaus.jackson clearly works.
import java.io.File;
import skf.config.Configuration;
import skf.config.ConfigurationFactory;
import federate.ExcavatorFederate;
import federate.ExcavatorFederateAmbassador;
public class ExcavatorMain {
private static final File configurationFile = new File(
"testResources/configuration/conf.json");
public static void main(String[] args) {
System.out.println(configurationFile);
// TODO Auto-generated method stubz
ExcavatorFederateAmbassador fedamb = new ExcavatorFederateAmbassador();
ExcavatorFederate fed = new ExcavatorFederate(fedamb);
ConfigurationFactory factory = null;
Configuration config = null;
try {
factory = new ConfigurationFactory();
config = factory.importConfiguration(configurationFile);
fed.configureAndStart(config);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The exceptions were:
testResources\configuration\conf.json
Exception in thread "main" java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.<init>(Ljava/lang/Class;I)V
at org.codehaus.jackson.map.type.TypeBase.<init>(TypeBase.java:34)
at org.codehaus.jackson.map.type.SimpleType.<init>(SimpleType.java:46)
at org.codehaus.jackson.map.type.SimpleType.constructUnsafe(SimpleType.java:63)
at org.codehaus.jackson.map.ObjectMapper.<clinit>(ObjectMapper.java:183)
at skf.config.ConfigurationFactory.getObjectMapper(ConfigurationFactory.java:72)
at skf.config.ConfigurationFactory.importConfiguration(ConfigurationFactory.java:53)
at ExcavatorMain.main(ExcavatorMain.java:25)
I don't get it.

Related

Error: Unable to initialize main class app.ExpressionApp Caused by: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/tree/ParseTree

I got this error message have been browsing for a long time and still couldn't find the reason or the fix for it.
Here is the Error message I am getting:
Error: Unable to initialize main class app.ExpressionApp Caused by: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/tree/ParseTree
Here is the image of my directories (the folder I am trying to run is the visitor one):
And here is the ExpressionApp class content
package app;
import java.io.IOException;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import Expression.AntlrToProgram;
import Expression.ExpressionProcessor;
import Expression.Program;
import antlr.ExprLexer;
import antlr.ExprParser;
public class ExpressionApp {
public static void main(String[] args) {
if(args.length != 1) {
System.err.print("Usage: file name");
}
else {
String fileName = args[0];
ExprParser parser = getParser(fileName);
ParseTree antlrAST = parser.prog();
AntlrToProgram progVisitor = new AntlrToProgram();
Program prog = progVisitor.visit(antlrAST);
if(progVisitor.semanticErrors.isEmpty()) {
ExpressionProcessor ep = new ExpressionProcessor(prog.expressions);
for(String evaluation: ep.getEvaluationResults()) {
System.out.println(evaluation);
}
}
else {
for(String err: progVisitor.semanticErrors) {
System.out.println(err);
}
}
}
}
private static ExprParser getParser(String fileName) {
ExprParser parser = null;
try {
CharStream input = CharStreams.fromFileName(fileName);
ExprLexer lexer = new ExprLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
parser = new ExprParser(tokens);
} catch (IOException e) {
e.printStackTrace();
}
return parser;
}
}
I have checked whether I have imported any wrong antlr files and nothing was foud

Box Java sdk - The API returned an error code 404 - Not Found

I need to create a utility, which downloads the files from the box folder. But I am unable to get it working though:
package com.box.sdk.example;
import com.box.sdk.BoxConfig;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import com.box.sdk.BoxFile;
import com.box.sdk.BoxFolder;
import com.box.sdk.BoxItem;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PlayGround {
public static void main(String[] args) {
Path configPath = Paths.get("config.json");
Path currentDir = Paths.get("").toAbsolutePath();
try (BufferedReader reader = Files.newBufferedReader(configPath, Charset.forName("UTF-8"))) {
BoxConfig boxConfig = BoxConfig.readFrom(reader);
BoxDeveloperEditionAPIConnection client = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig);
String folderId = "125601757844";
BoxFolder folder = new BoxFolder(client, folderId);
String folderName = folder.getInfo().getName();
Path localFolderPath = currentDir.resolve(Paths.get(folderName));
if (!Files.exists(localFolderPath)) {
localFolderPath = Files.createDirectory(localFolderPath);
} else {
localFolderPath = resetLocalFolder(localFolderPath);
}
for (BoxItem.Info itemInfo : folder) {
if (itemInfo instanceof BoxFile.Info) {
BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
BoxFile file = new BoxFile(client, fileInfo.getID());
String localFilePath = localFolderPath.resolve(Paths.get(fileInfo.getName())).toAbsolutePath()
.toString();
FileOutputStream stream = new FileOutputStream(localFilePath);
file.download(stream);
stream.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static Path resetLocalFolder(Path localFolderPath) throws IOException {
Files.list(localFolderPath).forEach(file -> {
System.out.println(file.getFileName());
try {
Files.delete(file.toAbsolutePath());
} catch (IOException e) {
}
});
Files.delete(localFolderPath);
localFolderPath = Files.createDirectory(localFolderPath);
return localFolderPath;
}
}
When I run this code, I get the following exception:
Exception in thread "main" com.box.sdk.BoxAPIResponseException: The API returned an error code [404 | lsgp4zgkfg6qipxg.0d094ed7daa5f78921603840e0fa470e1] not_found - Not Found
at com.box.sdk.BoxAPIResponse.<init>(BoxAPIResponse.java:92)
at com.box.sdk.BoxJSONResponse.<init>(BoxJSONResponse.java:32)
at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:680)
at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:382)
at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:349)
at com.box.sdk.BoxFolder.getInfo(BoxFolder.java:289)
at com.box.sdk.example.PlayGround.main(PlayGround.java:26)
Note: I am able to run the above code, by using developer token, which lasts for 1 hour, but I can't build my production application on such volatile code.
BoxAPIConnection client = new BoxAPIConnection("3i8b5sPnxUotd5etDuUkzGjXXzBphty9");

Reading XSD from URL using Java

Objective : I want to read a WSDL and print the services in the WSDL, complex types and Complex type definitions.
Worked : I've used WSDL4J for reading WSDL and successfully able to print the services and their parameters (complex types). Now I want to read the complex type definitions which is available in XSD. I'm unable to read XSD .Is ther any way to do it ?
I'm getting XSModel as null
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.wsdl.BindingOperation;
import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.xml.WSDLReader;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import com.ibm.wsdl.BindingImpl;
import com.ibm.wsdl.xml.WSDLReaderImpl;
import com.sun.org.apache.xerces.internal.impl.xs.XSImplementationImpl;
import com.sun.org.apache.xerces.internal.xs.XSLoader;
import com.sun.org.apache.xerces.internal.xs.XSModel;
public class WSDLDetails {
public static void main(String[] args) {
try {
String wsdlURL = "https://abc.xyz.com/webservice/MessagingSevice?WSDL";
String xsdURL = "https://abc.xyz.com/webservice/MessagingSevice?xsd=1";
java.lang.System.setProperty("https.protocols", "TLSv1.2");
getAllBindingOperation(wsdlURL);
readXSD(xsdURL);
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<String> getAllBindingOperation(String wsdlUrl) {
List<BindingOperation> operationList = new ArrayList();
List<String> nameList = new ArrayList();
try {
WSDLReader reader = new WSDLReaderImpl();
reader.setFeature("javax.wsdl.verbose", false);
Definition definition = reader.readWSDL(wsdlUrl.toString());
Map<String, BindingImpl> defMap = definition.getAllBindings();
Collection<BindingImpl> collection = defMap.values();
for (BindingImpl binding : collection) {
operationList.addAll(binding.getBindingOperations());
}
for (BindingOperation operation:operationList) {
nameList.add(operation.getName());
System.out.println("Name :: " + operation.getName());
System.out.println("Request :: " + operation.getBindingInput());
System.out.println("Response :: " + operation.getBindingOutput());
}
} catch (WSDLException e) {
System.out.println("get wsdl operation fail.");
e.printStackTrace();
}
return nameList;
}
public static void readXSD(String xsdURL) {
try {
System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
com.sun.org.apache.xerces.internal.impl.xs.XSImplementationImpl impl = (XSImplementationImpl) registry.getDOMImplementation("XS-Loader");
XSLoader schemaLoader = impl.createXSLoader(null);
XSModel model = schemaLoader.loadURI(xsdURL);
System.out.println(model);
} catch (Exception e) {
e.printStackTrace();
}
}
You can use xsd2java plugin with maven
https://github.com/qaware/xsd2java-gradle-plugin
Here is an example showing how to retrieve the XSModel from an XSD URL, and print the complex types declared therein.
import org.apache.xerces.impl.xs.XMLSchemaLoader;
import org.apache.xerces.impl.xs.XSComplexTypeDecl;
import org.apache.xerces.impl.xs.XSElementDecl;
import org.apache.xerces.xs.XSConstants;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSNamedMap;
import org.apache.xerces.xs.XSTypeDefinition;
public class Test {
public static void main(String[] args) {
try {
String xsdURL = "http://fsharp.github.io/FSharp.Data/data/po.xsd";
XMLSchemaLoader xsLoader = new XMLSchemaLoader();
XSModel xsModel = xsLoader.loadURI(xsdURL);
// print global element declarations
System.out.println("\nGlobal Element Declarations:");
XSNamedMap globalElemDecls = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION);
globalElemDecls.forEach((k,v) -> System.out.println((XSElementDecl) v));
// print global complex type declarations
System.out.println("\nGlobal Complex Type Declarations:");
XSNamedMap globalComplexTypeDecls = xsModel.getComponents(XSTypeDefinition.COMPLEX_TYPE);
globalComplexTypeDecls.forEach((k,v) -> System.out.println((XSComplexTypeDecl) v));
} catch (Exception e) {
e.printStackTrace();
}
}
}
If you got null at xsLoader.loadURI(xsdURL), it is likely there are some flaws in the given XSD file. For example, "White spaces are required between publicId and systemId". You might need to fix these flaws first.

Retrieving properties from the Classpath rather than from resources/config.properties

I currently have the following method which allows me to pick properties that have been defined in my resources/config.properties file
private final Properties properties = new Properties();
{
final ClassLoader loader = getClass().getClassLoader();
try(InputStream config = loader.getResourceAsStream("Resources/config.properties")){
properties.load(config);
} catch(IOException e){
throw new IOError(e);
}
}
But I now want to pick my properties from the classpath so I have moved my config.properties from resources and placed it directly under src. But I'm struggling to know what changes I now need to make to my method to enable me to read from classpath.
Check example here
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReaderProp {
public final Properties properties = new Properties();
public ReaderProp()
{
final ClassLoader loader = getClass().getClassLoader();
try(InputStream config = loader.getResourceAsStream("error.properties")){
properties.load(config);
} catch(IOException e){
throw new IOError(e);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ReaderProp readerProp = new ReaderProp();
System.out.println(readerProp.properties.get("E1000_SE_ERROR-CODE"));// output E1000
}
}
Check error.properties
======================
E1000_SE_ERROR-CODE = E1000
//Find another answer
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReaderProp {
private final Properties configProp = new Properties();
/**
* read property file
*
* #param propertyName
* #param path
*
* #throws IOException
*/
public ReaderProp(String propertyName, String path) {
try {
InputStream in;
File file;
if (path.equals("")) {
in = this.getClass().getClassLoader().getResourceAsStream(propertyName);
}
else {
file = new File(path + propertyName);
in = new FileInputStream(file);
}
configProp.load(in);
}
catch (IOException e) {
}
}
public static void main(String[] args) {
ReaderProp readerProp = new ReaderProp("error.properties",new File("").getAbsolutePath()+"\\src\\");
System.out.println(readerProp.configProp.get("E1000_SE_ERROR-CODE"));// output E1000
}
}

kryo serializing of class (task object) in apache spark returns null while de-serialization

I am using java spark API to write some test application . I am using a class which doesn't extends serializable interface . So to make the application work I am using kryo serializer to serialize the class . But the problem which I observed while debugging was that during the de-serialization the returned class object becomes null and in turn throws a null pointer exception . It seems to be closure problem where things are going wrong but not sure.Since I am new to this kind of serialization I don't know where to start digging.
Here is the code I am testing :
package org.apache.spark.examples;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
/**
* Spark application to test the Serialization issue in spark
*/
public class Test {
static PrintWriter outputFileWriter;
static FileWriter file;
static JavaSparkContext ssc;
public static void main(String[] args) {
String inputFile = "/home/incubator-spark/examples/src/main/scala/org/apache/spark/examples/InputFile.txt";
String master = "local";
String jobName = "TestSerialization";
String sparkHome = "/home/test/Spark_Installation/spark-0.7.0";
String sparkJar = "/home/test/TestSerializationIssesInSpark/TestSparkSerIssueApp/target/TestSparkSerIssueApp-0.0.1-SNAPSHOT.jar";
SparkConf conf = new SparkConf();
conf.set("spark.closure.serializer","org.apache.spark.serializer.KryoSerializer");
conf.set("spark.kryo.registrator", "org.apache.spark.examples.MyRegistrator");
// create the Spark context
if(master.equals("local")){
ssc = new JavaSparkContext("local", jobName,conf);
//ssc = new JavaSparkContext("local", jobName);
} else {
ssc = new JavaSparkContext(master, jobName, sparkHome, sparkJar);
}
JavaRDD<String> testData = ssc.textFile(inputFile).cache();
final NotSerializableJavaClass notSerializableTestObject= new NotSerializableJavaClass("Hi ");
#SuppressWarnings({ "serial", "unchecked"})
JavaRDD<String> classificationResults = testData.map(
new Function<String, String>() {
#Override
public String call(String inputRecord) throws Exception {
if(!inputRecord.isEmpty()) {
//String[] pointDimensions = inputRecord.split(",");
String result = "";
try {
FileWriter file = new FileWriter("/home/test/TestSerializationIssesInSpark/results/test_result_" + (int) (Math.random() * 100));
PrintWriter outputFile = new PrintWriter(file);
InetAddress ip;
ip = InetAddress.getLocalHost();
outputFile.println("IP of the server: " + ip);
result = notSerializableTestObject.testMethod(inputRecord);
outputFile.println("Result: " + result);
outputFile.flush();
outputFile.close();
file.close();
} catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
return result;
} else {
System.out.println("End of elements in the stream.");
String result = "End of elements in the input data";
return result;
}
}
}).cache();
long processedRecords = classificationResults.count();
ssc.stop();
System.out.println("sssssssssss"+processedRecords);
}
}
Here is the KryoRegistrator class
package org.apache.spark.examples;
import org.apache.spark.serializer.KryoRegistrator;
import com.esotericsoftware.kryo.Kryo;
public class MyRegistrator implements KryoRegistrator {
public void registerClasses(Kryo kryo) {
kryo.register(NotSerializableJavaClass.class);
}
}
Here is the class I am serializing :
package org.apache.spark.examples;
public class NotSerializableJavaClass {
public String testVariable;
public NotSerializableJavaClass(String testVariable) {
super();
this.testVariable = testVariable;
}
public String testMethod(String vartoAppend){
return this.testVariable + vartoAppend;
}
}
This is because spark.closure.serializer only supports the Java serializer. See http://spark.apache.org/docs/latest/configuration.html about spark.closure.serializer

Categories

Resources