How to import org.codehaus.groovy.scriptom.* on Groovy? - groovy

I'm trying to run a Groovy app to manipulate Excel files on STS (by SpringSource) 2.3.0.
My Groovy version is 1.7.
Class:
package com.mytool
import org.codehaus.groovy.scriptom.ActiveXObject
/**
* #author Mulone
*
*/
class SurveyTool {
static main(args) {
print 'test'
def wshell = new ActiveXObject('Wscript.Shell')
wshell.popup("Scriptom is Groovy")
}
}
Sadly, this is what I get:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\workspace\SurveyTool\src\com\geoadapta\surveytool\SurveyTool.groovy: 6: unable to resolve class org.codehaus.groovy.scriptom.ActiveXObject
# line 6, column 1.
import org.codehaus.groovy.scriptom.ActiveXObject
^
1 error
I also tried to rename ActiveXObject to ActiveXProxy with the same result.
I tried to import scriptom manually from the package scriptom-all-assembly-1.6.0 but I didn't work.
Any idea?
Cheers

Ops, I fixed it by importing all the jar files manually and by putting jacob-1.14.3-x86.dll in the project folder.

Related

How to declare a Groovy class in one file and use it in a Groovy script in another file?

After reading many threads I am stuck on this very simple task.
I have a sandbox project with only two files :
src/main/groovy/classes/foo
package classes.foo
class Foo{
#Override
String toString() {
return "Foo"
}
}
and src/main/groovy/scripts/script.groovy
package scripts
import classes.Foo
def foo = new Foo()
print foo.toString()
Now if I run the command :
groovy src/main/groovy/scripts/script.groovy
I get the following error :
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
src\main\groovy\scripts\utils\script.groovy: 3: unable to resolve class classes.Foo
# line 3, column 1.
import classes.Foo
^
1 error
How can I use my class Foo in my script script.groovy ?
As suggested by dagget, I had to specify the classpath based on my root folder which in my case is groovy :
groovy -cp src/main/groovy src/main/groovy/scripts/script.groovy

Getting unknown type : import in groovy in Jenkins Pipeline

I am trying to create a Jenkins Pipeline Script using groovy. However, the import statement is giving me a compilation error - Unknown Type : Import. Not sure why.
You should define import jxl.* at the top of the pipeline script, e.g.
import jxl.*
node {
stage('Execute Tests') {
try {
dir('.') {
sh '......' // etc.
}
}
}
}
When you added it inside node {} block Jenkins was looking for a method instead of import class statement. The good convention is to put all import statements at the top of the Groovy file.

Cassandra 3.10 Create Trigger, class doesn't exist

I have DSE 5.1 with Cassandra 3.10 and cql 5.0.1 installed on CentOS 7.3.1611 (Core).
I'm following the tutorial from DSE Triggers:
http://docs.datastax.com/en/dse/5.1/cql/cql/cql_reference/cql_commands/cqlCreateTrigger.html?hl=trigger
Which send me to github:
https://github.com/apache/cassandra/tree/trunk/examples/triggers
According to the tutorial, I am compiling the jar with ANT 1.10.1. The compiled jar contain the code:
package org.apache.cassandra.triggers;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import org.apache.cassandra.schema.TableMetadata;
import org.apache.cassandra.schema.Schema;
import org.apache.cassandra.db.Mutation;
import org.apache.cassandra.db.partitions.Partition;
import org.apache.cassandra.db.partitions.PartitionUpdate;
import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.UUIDGen;
public class AuditTrigger implements ITrigger
{
private Properties properties = loadProperties();
public Collection<Mutation> augment(Partition update)
{
String auditKeyspace = properties.getProperty("keyspace");
String auditTable = properties.getProperty("table");
TableMetadata metadata = Schema.instance.getTableMetadata(auditKeyspace, auditTable);
PartitionUpdate.SimpleBuilder audit = PartitionUpdate.simpleBuilder(metadata, UUIDGen.getTimeUUID());
audit.row()
.add("keyspace_name", update.metadata().keyspace)
.add("table_name", update.metadata().table)
.add("primary_key", update.metadata().partitionKeyType.getString(update.partitionKey().getKey()));
return Collections.singletonList(audit.buildAsMutation());
}
private static Properties loadProperties()
{
Properties properties = new Properties();
InputStream stream = AuditTrigger.class.getClassLoader().getResourceAsStream("AuditTrigger.properties");
try
{
properties.load(stream);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
FileUtils.closeQuietly(stream);
}
return properties;
}
}
The compiled jar, I am copying to the cassandra triggers location (/etc/cassandra/triggers). Then I run from cql the command:
CREATE TRIGGER test1 ON test.test USING 'org.apache.cassandra.triggers.AuditTrigger';
And send me the error:
ConfigurationException: Trigger class 'org.apache.cassandra.triggers.AuditTrigger' doesn't exist
I copied the jar in all the nodes of my cluster, restarted the cluster and ran the command:
nodetool reloadtriggers
With no success, I also put in the jvm.options the command:
-Dcassandra.triggers_dir=/etc/dse/cassandra/triggers
And in the cassandra-env.sh, I added the command:
JVM_OPTS="$JVM_OPTS -Dcassandra.triggers_dir=/etc/dse/cassandra/triggers"
And I am getting the same error. I tryied adding the jar in the cassandra lib folder (/usr/share/dse/cassandra/lib/) and it seems to not been loading the compiled jar.
When I run the command in cql:
CREATE TRIGGER test1 ON test.test USING 'org.apache.cassandra.triggers.AuditTrigger';
I am still getting the same error
ConfigurationException: Trigger class 'org.apache.cassandra.triggers.AuditTrigger' doesn't exist
So, my question is, How can I make cassandra load my jar correctly and use it to create a trigger in cql?

Groovy importing static nested classes

The following two classes in the same package:
Imported.groovy
class Imported {
static class Inner {
}
}
Main.groovy
import Imported
class Main {
static main(args) {
new Imported.Inner()
}
}
When run:
$ groovy Main.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/Main.groovy: 5: unable to resolve class Imported.Inner
# line 5, column 5.
new Imported.Inner()
^
1 error
Any reason this is happening? How to properly import static nested classes?
Just compile Imported.groovy: groovyc Imported.groovy so that you have Imported.class and Imported$Inner.class.
Then simply invoke groovy Main.groovy and it should work.
If you want to have some "import/include" functionality, check Including a groovy script in another groovy and how to simply import a groovy file in another groovy script.

Neo4j Spatial: can't run spatial

I have been trying to work with Neo4j Spatial for my project, but I can't make it work.
With limited documentation and examples I figured out how to load OSM map to the database. But to check if it is loaded, I am trying to execute a spatial query.
While trying to run my code I get this error:
import.java:69: error: cannot access GremlinGroovyPipeline
.startIntersectSearch(layer, bbox)
^
class file for com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline not found
I understand what's wrong (it can't find the required library), but I don't know how to fix it. The reason is when I run Neo4j Spatial tests, LayerTest.java and TestSpatial.java do include GeoPipeline library and it works perfectly fine. However, when I created my simple java file to test Neo4j, and trying to execute commands that depend GeoPipeline library I get the error above.
I read the instructions on github for Neo4j and saw this note:
Note: neo4j-spatial has a mandatory dependency on
GremlinGroovyPipeline from the com.tinkerpop.gremlin.groovy package.
The dependency in neo4j is type 'provided', so when using
neo4j-spatial in your own Java project, make sure to add the following
dependency to your pom.xml, too.
However, I am not using Maven to build my app. It is a simple java file, that I want to run to test if I get how everything works.
here is the code from my java file:
package org.neo4j.gis.spatial;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.neo4j.Neo4jSpatialDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.neo4j.gis.spatial.osm.OSMDataset;
import org.neo4j.gis.spatial.osm.OSMDataset.Way;
import org.neo4j.gis.spatial.osm.OSMGeometryEncoder;
import org.neo4j.gis.spatial.osm.OSMImporter;
import org.neo4j.gis.spatial.osm.OSMLayer;
import org.neo4j.gis.spatial.osm.OSMRelation;
import org.neo4j.gis.spatial.pipes.osm.OSMGeoPipeline;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import org.neo4j.kernel.impl.batchinsert.BatchInserter;
import org.neo4j.kernel.impl.batchinsert.BatchInserterImpl;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.gis.spatial.pipes.GeoPipeline;
class SpatialOsmImport {
public static void main(String[] args)
{
OSMImporter importer = new OSMImporter("ott.osm");
Map<String, String> config = new HashMap<String, String>();
config.put("neostore.nodestore.db.mapped_memory", "90M" );
config.put("dump_configuration", "true");
config.put("use_memory_mapped_buffers", "true");
BatchInserter batchInserter = new BatchInserterImpl("target/dependency", config);
importer.setCharset(Charset.forName("UTF-8"));
try{
importer.importFile(batchInserter, "ott.osm", false);
batchInserter.shutdown();
GraphDatabaseService db = new EmbeddedGraphDatabase("target/dependency");
importer.reIndex(db, 10000);
db.shutdown();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
GraphDatabaseService database = new EmbeddedGraphDatabase("target/dependency");
try{
SpatialDatabaseService spatialService = new SpatialDatabaseService(database);
Layer layer = spatialService.getLayer("layer_roads");
LayerIndexReader spatialIndex = layer.getIndex();
System.out.println("Have " + spatialIndex.count() + " geometries in " + spatialIndex.getBoundingBox());
Envelope bbox = new Envelope(-75.80, 45.19, -75.7, 45.23);
// Search searchQuery = new SearchIntersectWindow(bbox);
// spatialIndex.executeSearch(searchQuery);
// List<SpatialDatabaseRecord> results = searchQuery.getResults();
List<SpatialDatabaseRecord> results = GeoPipeline
.startIntersectSearch(layer, bbox)
.toSpatialDatabaseRecordList();
doGeometryTestsOnResults(bbox, results);
} finally {
database.shutdown();
}
}
private static void doGeometryTestsOnResults(Envelope bbox, List<SpatialDatabaseRecord> results) {
System.out.println("Found " + results.size() + " geometries in " + bbox);
Geometry geometry = results.get(0).getGeometry();
System.out.println("First geometry is " + geometry);
geometry.buffer(2);
}
}
It is very simple right now, but I can't make it work. How do I include com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline in my app, so it works?
I run everything on Ubuntu 12.04 and java version "1.7.0_25", Java(TM) SE Runtime Environment (build 1.7.0_25-b15).
Any help is greatly appreciated.
the best way to get all the required dependencies in a place where you can include them in your classpath is to run
mvn dependency:copy-dependencies
in neo4j-spatial, and find the libs to include in target/deps, see http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

Resources