I'm trying to use ASTParser in eclipse but facing an NoClassDefFoundError.
I've already followed some guides and imported related 9 jars.
Following are the details:
package test_JDT;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
// import org.eclipse.equinox.common.*;
// import org.eclipse.core.jobs;
class tester {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello ast parser");
String javaFilePath = "C:\\Users\\tabzhang\\eclipse-workspace\\test_JDT\\src\\test_JDT/classdemo.java";
byte[] input = null;
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(javaFilePath));
input = new byte[bufferedInputStream.available()];
bufferedInputStream.read(input);
bufferedInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String str = new String(input);
System.out.println(str);
ASTParser astParser = ASTParser.newParser(AST.JLS3);
astParser.setSource(new String(input).toCharArray());
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
CompilationUnit result = (CompilationUnit) (astParser.createAST(null));
}
}
included jars
the error report
command line
How should I get the code running well?
In Project > Properties: Java Build Path, tab Libraries move all the JARs from the Modulepath to the Classpath.
Related
I have a a standalone java program and it read the data from REST end point and insert data into table in Server.
package com.test.main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.test.connectdb.ConDataBase;
import com.test.entity.User;
public class Main {
public static void main(String[] args) {
try {
URL url = new URL("https://jsonplaceholder.typicode.com/todos/");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
System.out.println("DONE2");
int responsecode = conn.getResponseCode();
String inline = "";
if(responsecode == 200){
Scanner sc = new Scanner(url.openStream());
while(sc.hasNext())
{
inline+=sc.nextLine();
}
System.out.println("JSON data in string format");
System.out.println(inline);
sc.close();
}else{
throw new RuntimeException("HttpResponseCode:" +responsecode);
}
//-----------------------------------------------------------------------
Connection con = new ConDataBase().buildConnection();
User[] userList = new Gson().fromJson(inline, User[].class);
System.out.println(userList.length);
for (User user : userList) {
//System.out.println(user.getCompleted());
String insert_date = "insert into XX_USER "
+ "(USER_ID)"
+ "VALUES"
+"('"+user.getCompleted()+"')";
try {
PreparedStatement ps_data = con.prepareStatement(insert_date);
ps_data.executeUpdate();
con.commit();
System.out.println("Successfully Inserted");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I need to run this jar file using PLSQL. That means I have transferred this jar file into Linux server path (/home/rest). Oracle database is installed in server. I need to run this jar using PLSQL. Is it possible?
Use the LOADJAVA utility to load the jar file and all other jar dependencies into Oracle's internal classpath (this is different from the operating system's class path).
You will probably also want to change your code to a static method without arguments (rather than main with a string array argument) as it will make invoking the method much simpler.
// package and imports
public class Main {
public static void yourMethodName() {
// your code
}
}
Then you need to use something like:
CREATE PROCEDURE get_todos_from_rest_service AS
LANGUAGE JAVA NAME 'com.test.main.Main.yourMethodName()';
To create a procedure wrapper around the java method which you can then invoke in PL/SQL.
A more detailed example can be found here: Database Java Developer's Guide - Java Stored Procedures Application Example
I'm stumped. This script works great in Eclipse on my windows machine, if I hard code the file paths. If I try to take in arguments and run it on my edge node (a linux box), it throws no particular errors but it just leaves an empty output file. I must be missing something stupid, but I am not seeing it. Anyone have any idea what's going on?
package com.trv.cbia.de.tika;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class DocParser {
public Map<String, Object> processRecord(String path) {
Map<String, Object> map = new HashMap<String, Object>();
String docPath = path;
try{
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
InputStream inputstream = new FileInputStream(new File(docPath));
ParseContext pcontext = new ParseContext();
Parser pdfparser = new AutoDetectParser();
pdfparser.parse(inputstream, handler, metadata, pcontext);
map.put("text", handler.toString().replaceAll("\n|\r|\t", " "));
map.put("title", metadata.get(TikaCoreProperties.TITLE));
map.put("pageCount", metadata.get("xmpTPg:NPages"));
} catch (IOException ex){
System.out.println("Caught IOException:" + ex.getMessage());
}
catch(TikaException tx) {
System.out.println("Caught TikaException: " + tx.getMessage());
}
catch(SAXException sx){
System.out.println("Caught SAXException: " + sx.getMessage());
}
return map;
}
public static void main(String args[]){
String file = args[0];
String out = args[1];
DocParser textExtract = new DocParser();
Map<String, Object> extractedMap = textExtract.processRecord(file);
try {
PrintWriter writer = new PrintWriter(out,"UTF-8");
writer.println(extractedMap.get("text"));
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
Ended up being that I needed to add tika-app-1.13.jar to my classpath. It never divulged any classpath errors. I had to dig through a bunch of the apache mailing list to find someone with a similar problem. Posting the solution here in case anyone else runs across it.
I am new to XML.After a lot of search i found XMLUnit to do that but the problem am getting is :
whenever i change the contents of xml file to simple string text and try to execute the code ,it still shows green in junit test which it should not do although it is throwing an exception but my requirement is the signal should be red even the files are not in proper xml format.Enclosing my work for so far.
package mypack;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.xml.sax.SAXException;
public class MyXMLTestCase extends XMLTestCase {
enter code here
#Test
public void testMyXmlTestCase() {
FileReader expected = null;
FileReader output = null;
try {
expected = new FileReader("D:/vivek.xml");
output = new FileReader("D:/rahul.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XMLUnit.setNormalizeWhitespace(Boolean.TRUE);
try {
Diff diff = new Diff(expected,output);
// assertTrue("XMLSimilar"+diff.toString(),diff.similar());
//assertTrue("XMLIdentical"+diff.toString(),diff.identical());
DetailedDiff mydiff = new DetailedDiff(diff);
List allDifferences = mydiff.getAllDifferences();
assertEquals(mydiff.toString(), 0, allDifferences.size());
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my code:
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import java.io.*;
public class CreateNewPresentation
{
public static void main(args[])
{
try
{
SlideShow slideShow = new SlideShow();
Slide slide = slideShow.createSlide();
FileOutputStream out = new
FileOutputStream("slideshow.ppt");
slideShow.write(out);
System.out.println("File Created...");
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The issue is that it does not recognise org.apache.poi package.
How can I make it work?
If you are using netbeans, refer this for setting classpath.
If you want to set classpath from command line refer this
I'm new to this kabeja package so please can some one provide code example or reading material to render PNG from DXF file using Java?
This is the sample code that generate PNG image from DXF file.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.parser.*;
import org.kabeja.parser.ParserBuilder;
import org.kabeja.svg.SVGGenerator;
import org.kabeja.xml.*;
public class MyClass {
public static void main(String[] args) {
MyClas x=new MyClas();
x.parseFile("C:\\Users\\Space\\Desktop\\test2.dxf");
}
public void parseFile(String sourceFile) {
try {
FileOutputStream o=new FileOutputStream("C:\\Users\\Space\\Desktop\\test2.png");
InputStream in = new FileInputStream(sourceFile);//your stream from upload or somewhere
Parser dxfParser = ParserBuilder.createDefaultParser();
dxfParser.parse(in, "");
DXFDocument doc = dxfParser.getDocument();
SVGGenerator generator = new SVGGenerator();
//org.xml.sax.InputSource out = SAXPNGSerializer;
SAXSerializer out = new org.kabeja.batik.tools.SAXPNGSerializer();
out.setOutput(o);
generator.generate(doc,out,new HashMap());
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
Hope you get what you required :)