How to run Neo4j with OSM and Neo4jSpatial? - openstreetmap

Hello i'm new in neo4j and i would like to use OSM + Neo4j Spatial.
I have a maven project and my Neo4j version is 2.3.0-M01
I have a simple code just for importing an OSM file but it displays some errors in the import files: GraphDatabaseService, EmbeddedGraphDatabase and BatchInserter.
package testOSM;
import java.nio.charset.Charset;
import org.neo4j.gis.spatial.osm.OSMImporter;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.kernel.impl.batchinsert.BatchInserter;
public class TestOsm {
private static final String DB_PATH = "/community/data/graph.db";
public static void main(final String[] args){
OSMImporter importer = new OSMImporter("clz_map.osm");
importer.setCharset(Charset.forName("UTF-8"));
BatchInserter batchInserter = BatchInserter.inserter(DB_PATH);
try{
importer.importFile(batchInserter, "clz_map.osm", false);
GraphDatabaseService db = new EmbeddedGraphDatabase(DB_PATH);
importer.reIndex(db);
db.shutdown();
}
catch(Exception e){
System.out.println(e.getMessage());
}
batchInserter.shutdown();
}
}
May be my problem is with the versions, because i'm using Neo4j 2.3-M01, but i don't know exactly how should i set the versions e.g. here
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-graph-collections</artifactId>
<version>0.7.1-neo4j-2.0.2-SNAPSHOT</version>
<type>jar</type>
</dependency>
My pom.xml is based on https://github.com/neo4j-contrib/spatial/blob/master/pom.xml
Plus:
<repository>
<id>neo4j</id>
<url>http://m2.neo4j.org/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.3.0-M01</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>2.3.0-M01</version>
</dependency>
You can have a look into my git repository
https://github.com/amhg/OSM
Thank you in advance!

For anyone else that runs into this, here is how to do it: https://github.com/maxdemarzi/OSM
Notice a small differences between 2.2.x and 2.3 (7/30-currently on M2).
Just needed the right dependencies.

there are API changes since the last released version. Looking at https://github.com/neo4j-contrib/spatial/blob/master/pom.xml#L4 , it seems you can use Neo4j 2.2.3 if you build that project yourself with
mvn install
and then include version 0.15-neo4j-2.2.3 of the spatial plugin into your pom.xml from the local mvn repo.

I took a look at your pom.xml and it looks like you copied the pom.xml from Neo4j Spatial. This is not what you want.
Since you are trying to write a new application that uses Neo4j Spatial, you should have a pom that is new and refers to neo4j-spatial as a dependency, not a pom that is in any way similar to the neo4j-spatial pom. There is a section in the README that describes how to add neo4j-spatial as a dependency to your own pom.
So I would suggest you do the following:
create a new pom.xml - https://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project
then add the dependency as described in https://github.com/neo4j-contrib/spatial#using-neo4j-spatial-in-your-java-project-with-maven

Related

java.lang.NoClassDefFoundError: javax/xml/transform/TransformerConfigurationException

I am using dummy html string and trying to create the pdf from that...
once it tries to create ITextRenderer object, I am getting the "java.lang.NoClassDefFoundError: javax/xml/transform/TransformerConfigurationException"
Document doc = Jsoup.parse("<html><head><title>Pdf Generation..!</title></head><body><p>Pdf generated using flying saucer pdf openpdf!!!!</p></body></html>","UTF-8");
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
try (OutputStream os = new FileOutputStream("output.pdf")){
ITextRenderer renderer = new ITextRenderer();
SharedContext cntxt = renderer.getSharedContext();
cntxt.setPrint(true);
cntxt.setInteractive(false);
renderer.setDocumentFromString(doc.html(), "");
renderer.layout();
renderer.createPDF(os);
logger.info("PDF Generation using OpenPDF Done Successfully!!!");
}
catch(Exception ex){
ex.printStackTrace();
}
This is a maven archetype project and dependencies used for this are,
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-openpdf</artifactId>
<version>9.1.20</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.20</version>
</dependency>
I have looked through some of the shared suggestions, but none of it could resolve this...
Check the Import-Package directive in the BND Maven plugin. Either import javax.xml.transform explicitly or import everything (*)
javax.xml.transform.* is a provided API which is present in many bundle definitions. The correct Import-Package should include something like this:
javax.xml.transform,version=2.1.0 from org.apache.felix.framework (0)

Error while creating building a hibernate app without Maven

I am learning to hibernate so I made a small app for crud operation using mySQL as database. However, I am getting some errors and I cannot find the solution anywhere. SDK 17.0.2, I am not using maven , Also all hibernate final jar files have been added
my class:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Student;
public class CreateStudentDemo {
public static void main(String[] args) {
// Create session factory
SessionFactory factory= new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
//Create session
Session session = factory.getCurrentSession();
try {
//Create a Student object
System.out.println("Creating new student");
Student tempStudent = new Student("Pau;", "Wall", "paul#luv2code.com");
//Start a transaction
session.beginTransaction();
//save the student object
session.save(tempStudent);
//commit transaction
session.getTransaction().commit();
}
finally {
factory.close();
}
}
}
runtime error :
java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
java removed java.xml.bind from JAVA 9 and higher editions.
You need to add these jar files manually to your library
basically you can to that in this way.
If you use Intellij this is the easyest way:
jaxb-impl-2.3.0.jar
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
jaxb-core-2.3.0.jar
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
jaxb-api-2.3.1.jar
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
javax.activation-api-1.2.0.jar
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
If you use intellij here is a step my step to get these files through IDE
go to project structure:
Libraries, and click + sign
Chick to from Maven:
Paste these dependencies to the search.
It will automatically find exact jar files to download, Kist Paste it and click to the enter that is it.
Also later you can just add these files to the classpath just in case:
java removed java.xml.bind from JAVA 9 and higher editions.
Here is the eclipse solution:
In Eclipse IDE:
Download these jar files and paste them inside lib folder:
Go the project properties: The last line
Done these steps:
Happy Coding! :)

open api swagger-ui giving white label error page (404) after moving the project as a module in a project

I have the working project in Spring boot which was a standalone app. Now, to modularize, I have split the app and put the api in a module. Earlier the swagger-ui.html page was working, but after this, it is not working.
my pom.xml has these 2 dependencies
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>1.5.1</version>
</dependency>
My Application.java has the following code
#Bean
#Profile("!prod")
public OpenAPI caffeAPIRegistry() {
return new OpenAPI()
.info(new Info().title("My Awesome API")
.description("Powering UI")
.version("v3.0.0")
.license(new License().name("google Inc.,").url("https://google.com")));
}
The pom.xml fragment for the parent project mapping
<parent>
<artifactId>API_Project</artifactId>
<groupId>com.abc</groupId>
<version>2.0.0.RELEASE</version>
</parent>
I have been searching for a long time in internet if moving api as a module might require any other change, but could not find any help.
Are the interfaces with the REST annotations in a different package? if yes then that is the issue.

Maven Exec plugin, ClassLoader ClassNotFoundException

I've tried many suggestions on StackOverflow, and tested with many combinations of setup and pom.xml for 3 days, none of them works. Please help.
I started with a big project with a lot of dependencies, spring-boot, hibernate, etc. Then I create another small console project which import and use some classes from the big project. They are not parent and child project. All I do is add <dependency/> to the child project pom, like this.
P.S. the big project has a <parent/> spring-boot-starter-parent and use spring-boot-maven-plugin.
<dependency>
<groupId>myGroup</groupId>
<artifactId>bigProject</artifactId>
<version>1.0.1</version>
</dependency>
This works on Eclipse with m2e, I just use "Run as" java application and the small project works.
But I need to upload and run those project on my linux VM, which does not use GUI and Eclipse, only terminal.
Then after some reading, I try to use maven exec plugin to run the small project. My steps:
1. do mvn clean install on the big project, confirmed that it appears in my /.m2, local repository.
2. run mvn compile on small project
3. run mvn exec:java on small project
It fails on step 2, those import ... in the class of small project throw package xxx does not exist. maven fail with compilation error.
Then, I try to simplify the problem. I create two test projects with only 1 class, myLib and HelloWorld console application, then I add myLib dependency (pom). HelloWorld project to print a message from the class in myLib package. run step 1 to 3. It works.
public class App
{
public static void main( String[] args )
{
//SystemLog from big project, does not work
//SystemLog log = new SystemLog();
//log.setValue("test value");
//System.out.println(log.getValue());
//CustomMessage from myLib, works fine
CustomMessage cm = new CustomMessage();
cm.setTheMessage("custom message");
System.out.println(cm.getTheMessage() );
System.out.println(CustomMessage.defaultMsg);
}
}
pom.xml of small project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>dependOnOthers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dependOnOthers</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>myLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>bigproject</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>test.dependOnOthers.App</mainClass>
<!-- <arguments> <argument>argument1</argument> </arguments> -->
<!-- <arguments> <argument>-classpath</argument> <argument>target/lib/bigproject-1.0.1.jar</argument>
</arguments> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I add a reference to big project, try to print a log message using HelloWorld, it pass step 2, mvn compile, but when I do step 3, mvn exec:java, java.lang.ClassLoader.loadClass() throw ClassNotFoundException on the line where I new an SystemLog() instance, the class defined in big project.
Summarize:
Goal: try to run a console application using Maven exec plugin, which has a dependency on another project, that is installed in local repo /.m2
The SystemLog class is a model class with hibernate annotations.
1. small project depend on big project, fail on mvn compile, package does not exist.
2. HelloWorld depend on myLib, works fine.
3. HelloWorld depend on big project, fail at runtime with ClassNotFoundException.
It's a little late but i ran in the same error, migrating a project from 1.5.3 to 2.1.4
I have found a solution for this problem.
Spring Boot has changed the jar structure in version 2.0.0+, so you don't need to specify your class as <mainClass>test.dependOnOthers.App</mainClass>, you need to point JarLauncher as main entry point.
When the plugin will execute the jar, java will call the main method JarLauncher and several code of spring will be called after that he reads the manifest and call the class definied in Start-Class:. in your case test.dependOnOthers.App.
<configuration>
<!-- Since version 2 of spring, the jar is packed in
another way and has his main method here, don't worry in the MANIFEST file is described which file to boot -->
<mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
The old strucutre of Spring 1.0, was the classical java way todo it, so the java classloader can load it without problems and refer to the class when it is in the classpath, with the version 2.0.0+, they copy user class files and dependency into BOOT-INF, so the java classloader cannot load it anymore, you need to use the Spring classloader to get the class instance.
I have a workaround and know what is causing the problem, but don't know all the details, hope that someone will comment on this, or elaborate the answer.
The thing that cause this problem is spring-boot-maven-plugin, which I use to run spring-boot embedded tomcat server, using mvn spring-boot:run. This plugin seems to change something on the .jar file, maybe the manifest?
The workaround is to remove this plugin, run mvn clean install, that .jar installed in local repository will work correctly. run mvn compile on the small project. Then add the plugin back to big project, run mvn spring-boot:run
I do not know what the plugin changes that result in a .jar with package not exist, does it changes package name?
My workaround is clumsy, if there is a way to choose to compile with or without spring-boot-maven-plugin by defining maven goal, so that I do not need to change the pom for different build (with/without plugin). That would be a better solution.

Eclipse cannot resolve org.springframework.extensions.*

I should develope a java backed web script for alfresco but some libraries are missing on the classpath so most of the classes I like to use can not be resolved. I can build the project but Eclipse can not find the classes:
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
Where can I download the jar to add it to the classpath? I already searched and googled but I cannot find a download :/
if you are using maven ,add this to your pom.xml
<dependency>
<groupId>org.springframework.extensions.surf</groupId>
<artifactId>spring-webscripts</artifactId>
<version>1.0.0.M3</version>
</dependency>
<dependency>
<groupId>org.springframework.extensions.surf</groupId>
<artifactId>spring-webscripts-api</artifactId>
<version>1.0.0.M3</version>
</dependency>