How to use GeoScript groovy library inside Java project? - java

I have spring/postgis service with some spatial capabilities, but I haven't found Java library that can interpolate point to a linestring.
I'd like to to use this geoscript library to do that, but I'm not sure how to do that.
I've read about how to dynamically load groovy class in Java code, but I'm not sure how to do that with external library code. Should I just add this geoscript to build.gradle?
Or maybe there is some more efficient ways to do that?

I just gave it a try and it worked flawlessly. In the end a compiled Groovy library is just Java byte code.
Citing from Groovy in Action, Chapter 1.1.2 Playing nicely with Java: seamless integration:
Groovy is only
a new way of creating ordinary Java classes—
from a runtime perspective, Groovy is Java
with an additional JAR file as a dependency.
So add these dependencies
compile 'org.codehaus.groovy:groovy-all:2.4.5'
compile 'org.geoscript:geoscript-groovy:1.6.0'
and this repository
maven {
url "http://repo.boundlessgeo.com/main"
}
to your gradle build file and you are good to go.
This is the Java code I tried:
import geoscript.geom.LineString;
import geoscript.geom.Point;
public class Main {
public static void main(String[] args) {
LineString line = new LineString(new Point(1,2), new Point(3,4), new Point(4,5));
Point point = line.interpolatePoint(0.5);
System.out.println(point); // printed POINT (2.5 3.5)
}
}

Related

Replace jar and redirect import statements

We have a Java program that relies on a specific library. We have created a second library that has a very similar API to the first library, however, this one is made in-house and we are ready to begin testing it.
To test, we would like to replace the jar in the Java program with the jar of our new library. The issue is that the new library does not have the exact same namespace, so the import statements will not align. For example,
Java program
import someLibrary.x.y.Foo;
public class Main {
public static void main(String[] args){
new Foo().bar();
}
}
New Library has the same API but different namespace
anotherLibrary.x.y.Foo;
Question: How can I use the classloader or another tool to run a Java program but replace a dependency and redirect import statements to another namespace?
[EDIT] - We do not have access to the Java program's source code. We can have this program changed to use our new library but we do not want to do that until after it has been thoroughly tested.
The only solution I can think of would involve writing a custom ClassLoader that would alter the bytecode to change the method references and field references to change the class name.
How about the straightforward solution:
Create a branch of your main program (in git or whatever source control tool you use):
Apply all the changes required to work with the new library (change all the imports)
Deploy on test environment and test extensively
Merge back to master when you feel confident enough
Another solution could be:
Create a branch out of new library
Change the imports so that it will look exactly as the old one (with all the packages)
Substitute the old library with a new one in your application
Deploy on test environment and test extensively
When you're ready with the new library deploy to production and keep working in production for a grace period of month or something (until you really feel confident)
In a month change back all the imports (basically move from branch with the "old" imports to the branch with your real imports in both library and application.
Update
Its also possible to relocate packages of your version of the library automatically if you use maven.
Maven shade plugin has relocate goal that can be used to "relocate" the packages of your library to be just like packages of existing library. See shade plugin's documentation

MarkLogic using JavaAPI

I have been trying a normal Java API using the same example as mentioned in this page. I had to download various JARs:
java-client-api-3.0.7.jar
slf4j-api-1.7.25.jar
com.fasterxml.jackson.databind.jar
jackson-core-2.2.0-rc1.jar
apache-httpcomponents-httpclient.jar
javax.ws.rs.jar
It still gives me NoClassDefFoundError on
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/params/HttpParams at com.marklogic.client.DatabaseClientFactory.newClientImpl(DatabaseClientFactory.java:322)
My Code:
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.document.*;
import com.marklogic.client.io.*;
public class JavaML {
public static void main(String args[]) {
System.out.println("Here");
DatabaseClient client =
DatabaseClientFactory.newClient(
"localhost", 8000,"admin", "######",
DatabaseClientFactory.Authentication.DIGEST);
JSONDocumentManager docMgr = client.newJSONDocumentManager();
docMgr.write("/afternoon-drink",
new StringHandle("{name: \"Iced Mocha\", size: \"Grandé\", tasty: true}"));
String doc = docMgr.read("/afternoon-drink", new StringHandle()).get();
//System.out.println(doc);
}
}
Run:
javac -cp .;%CLASSES% JavaML.java
java -cp .;%CLASSES% JavaML
%CLASSES% - path to all the jars mentioned above.
Please help - am I missing something? Or is it a mandate that I have to download all these JARs?
It appears from the error message that you are missing the httpcore-4.1.jar.
However, there are a number of other dependencies that you are likely to be missing as well.
If you are maintaining your own set of dependencies and building your classpath manually, then you should download the Java API zip, which includes all of the necessary dependencies in the /lib directory.
However, it would be much easier if you were to use a build/dependency management tool, such as Gradle or Maven. If you have the Java API as a dependency, it will download all of it's dependencies and will compile your application with the necessary libraries on your classpath. It makes upgrades much easier, facilitates automation, and is a best practice in favor of manually collecting and organizing dependencies.

How can I create and Import Libraries in Netbeans - A Full Documented Q

It's me again. So here's the deal, I still don’t really grasp if I am doing something wrong.
I am trying to create my own Personal Library, in which I have certain string methods and so on.
I’ve started by creating a new Project as a Java library:
Then I've added a package to that library:
After that I've created a class in said package:
Here the code of my class:
And cleaned and built that Class//Library. Run -> Clean and Build
And afterwards created the JavaDoc Run -> Generate JavaDoc(MyTestLibrary)
After doing this i decided to add my newly created Library to the Libraries under Tools - > Libraries:
After that I’ve added a new Library (bottom right) and named it TestLibrary:
SO decided to add the .jar File first. I clicked on the “Add JAR/Folder... ” Option and searched for my Projekt Folder (from MyTestLibrary) and went in to the “dist” folder and selected the .jar file:
After that I’ve added the “src”-Folder in “Sources” and the “javadoc”-Folder in “Javadoc”.
After i was finished it was time to create a new Project, to be precise a new Java Application, and try to import my freshly created library. I rightclicked on Libraries and clicked on “Add Library...”:
And imported my Test Library and adding it:
Happy that I've added my Library to my application I decided to import it
import TestPackage.TestClass;.
If I try
public static void main(String[] args)
{
TestClass.
}
The only suggestion is TestClass.class and not TestClass.TestMethod.
Why can't I access the methods of the “TestClass” directly?
How can I see them?
I've tried being as specific as possible. I hope this time I can fully understand what is going on and why I can't access them.
The solution was fairly simple and obvious.
To access the methods without the need of creating an instance of the class i just had to declare them static.

How to implement a java library in Robot Framework

How can I create a library in Eclipse and then import it in Robot FrameWork?
I am searching a lot now and none of the guides out to help me out.
You need to do the following:
Create your java library
Add it to the classpath when running robot framework jython edition
Creating your java library:
Define a new java class. At this point try not to use a constructor yet (although it is possible to support constructors with fields)
Define the ROBOT_LIBRARY_SCOPE static String variable in the class.
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
Define public methods (not static) that will be used as the keywords
Adding your library to the class path
Compile your classes - ideally to a jar
Add the jar to the class path when running jython. The easiest way to do this is with the MVN Robot Framework plugin. Another option is to wrap the jybot run in a batch file and add CLASSPATH definition to it. There are other options as well (gradle or ant for example).
Using your library in your code
You need to import your library using the full package path
import library org.robot.sample.keywords.MyLibrary
https://blog.codecentric.de/en/2012/06/robot-framework-tutorial-writing-keyword-libraries-in-java/
You can see the full example of how to add a jar when using ride in this article
https://blog.codecentric.de/en/2012/04/robot-framework-tutorial-a-complete-example/

Can Play 2.3.x be used without Activator (and with maven)?

I have two related questions here.
In Play 2.2.x, the distribution was bundled as a zip file, and available for download through the maven repository http://downloads.typesafe.com/play/2.2.x/play-2.2.x.zip. This meant that you could use a pom.xml and embed play into your app without needing to use sbt. Given 2.3.x has shifted to the activator model, is it still possible to use it with maven?
And secondly, is it possible to use play 2.3.x without activator at all? (I know they have a sbt plugin for play, but that seems very complex as well).
Thanks!
Activator is only needed to create the empty template project, which you could also do by hand if you know a bit about play. After that empty project is created all you need is sbt (which actually is a pretty central part of activator).
With play 2.3 the distribution model changed from the one big zip-file to regular ivy/maven dependencies, so you could possibly get all dependencies right from a maven project. The problem is that the sbt play setup does so much more: template compilation, routes DSL compilation, hot reloading, asset pipeline stuff, so I don't think maven actually is an option.
Yes.
Example on Github
package io.github.alancnet
import java.io.File
import play.api.{Environment, ApplicationLoader}
object PlayTest {
class Dummy{}
def main(args:Array[String]):Unit = {
def startWebServer = {
val environment = new Environment(
new File("."),
classOf[Dummy].getClassLoader,
play.api.Mode.Dev
)
val context = play.api.ApplicationLoader.createContext(environment)
val application = ApplicationLoader(context).load(context)
play.api.Play.start(application)
play.core.server.NettyServer.fromApplication(
application
)
}
startWebServer
}
}

Categories

Resources