GraphStream - No valid display found - java

I'm following this official tutorial of graphStream.
And I'm trying to run this example code taken from there:
import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
public class Tutorial1 {
public static void main(String args[]) {
Graph graph = new SingleGraph("Tutorial 1");
graph.addNode("A");
graph.addNode("B");
graph.addNode("C");
graph.addEdge("AB", "A", "B");
graph.addEdge("BC", "B", "C");
graph.addEdge("CA", "C", "A");
graph.display();
}
}
And I'm getting the following error:
Exception in thread "main" java.lang.RuntimeException: Cannot launch viewer.
at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:212)
at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:204)
at org.test.Test.main(Test.java:23)
Caused by: org.graphstream.util.MissingDisplayException: No valid display found. Please check your System.setProperty("org.graphstream.ui") statement.
at org.graphstream.util.Display.getDefault(Display.java:79)
at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:209)
... 2 more
I did exactly what the tutorial showed.
what am I missing?
what does "No valid display found." means

If you are doing this in a Java application, you need to use version 1.3.
Add the following dependencies. Version 1.3
gs-core
gs-algo
gs-ui
Main
import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
public class Main {
public static void main(String args[]) {
Graph graph = new SingleGraph("Tutorial 1");
graph.setStrict(false);
graph.setAutoCreate( true );
graph.addNode("A");
graph.addNode("B");
graph.addNode("C");
graph.addEdge("AB", "A", "B");
graph.addEdge("BC", "B", "C");
graph.addEdge("CA", "C", "A");
graph.display();
}
}
POM
<?xml version="1.0" encoding="UTF-8"?>
<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>sed.home</groupId>
<artifactId>JavaTestArea</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-core</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-algo</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-ui</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
</project>
Output
If you use version 2.0, it needs to be done in a JavaFX, Swing, or Android application.

Related

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniTest in java.library.path Eclipse

I am trying a Maven Project with javacpp from Eclipse, and the project builds, but when I try to run it, it gives an exception and terminates itself. I am pretty new to Maven and managing pom.xml files, and I don't know what to do. The exception is:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniTest in java.library.path:
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2447)
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:809)
at java.base/java.lang.System.loadLibrary(System.java:1893)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1683)
at org.bytedeco.javacpp.Loader.load(Loader.java:1300)
at org.bytedeco.javacpp.Loader.load(Loader.java:1123)
at com.berk.maventest.Test.<clinit>(Test.java:13)
Below is my pom.xml file:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.berk</groupId>
<artifactId>maventest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp-platform</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.3.1-1.5.4</version>
</dependency>
</dependencies>
</project>
And my Test.java file:
package com.berk.maventest;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
#Platform(include = "Test.cpp")
#Namespace("TestLibrary")
public class Test extends Pointer{
static {
Loader.load();
}
public Test()
{
allocate();
}
private native void allocate();
public native int testMethod(int a);
public static void main(String [] args) {
//Calling the constructor
Test test = new Test();
//Calling the function
System.out.println("The answer is: " + test.testMethod(2));
//Calling the destructor
test.close();
}
}

Cannot resolve symbol 'TestUnit' in maven project

I'm trying to learn JUnit Testing currently. I want to test first custom 'TestUnit' class but IDE tells me that Cannot resolve symbol 'TestUnit'. What I should do to fix this error?
TestUnit class:
package test.lessons.purejava;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestUnit {
#Test
public void testAdd(){
String string = "Test unit works";
assertEquals("Test unit works", string);
}
#Test
public void concatTest(){
Concatenation concatenation = new Concatenation();
String result = concatenation.concat("Hello", " World");
assertEquals("Hello World", result);
}
}
and pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.lessons</groupId>
<artifactId>purejava</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!--<!–
https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
–>-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
</project>

Maven Netbeans and Vget Integration (Can't find Vget from maven)

My porm.xml file looks like this...
<?xml version="1.0" encoding="UTF-8"?>
<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>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
And My java code looks like this, But it is not adding the Vget from the maven to its import list...
package com.mycompany.mavenproject1;
import java.io.File;
import java.net.URL;
public class Downloader {
public static void main(String[] args) {
try {
// ex: http://www.youtube.com/watch?v=Nj6PFaDmp6c
String url = "https://www.youtube.com/watch?v=SCOKysMnH50";
// ex: "/Users/axet/Downloads"
String path = "";
VGet v = new VGet(new URL(url), new File(path));
v.download();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
The Line VGet v = new ... shows an error because the vget is not imported in the code and it is not imported because, the maven won't allow me to import it, I don't know what to do with it...

spark & java COMPILATION ERROR

When I used maven to compile spark java program, I got an compilation error like this
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/spark/java/src/main/java/SimpleApp.java:[9,36] cannot find symbol
symbol: variable read
location: variable spark of type org.apache.spark.sql.SparkSession
here is my JAVA pragram
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.Dataset;
public class SimpleApp {
public static void main(String[] args) {
String logFile = "/home/spark/spark-2.2.0-bin-hadoop2.7/README.md"; // Should be some file on your system
SparkSession spark = SparkSession.builder().appName("Simple Application").getOrCreate();
Dataset<String> logData = spark.read.textFile(logFile).cache();
// Dataset<String> logData = SparkSession.builder().appName("Simple Application").getOrCreate().read.textFile(logFile).cache();
long numAs = logData.filter(s -> s.contains("a")).count();
long numBs = logData.filter(s -> s.contains("b")).count();
System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);
spark.stop();
}
}
I write the program follow the official website :
http://spark.apache.org/docs/latest/quick-start.html
how this error comes..??
here is my pom.xml
<project>
<groupId>edu.berkeley</groupId>
<artifactId>simple-project</artifactId>
<modelVersion>4.0.0</modelVersion>
<name>Simple Project</name>
<packaging>jar</packaging>
<version>1.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency> <!-- Spark dependency -->
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
</project>
ok... I found the result..
the example in offical website is wrong ..
spark.read.textFile(logFile).cache(); --> spark.read().textFile(logFile).cache();
read should be read()

dom4j.Node iterate maps of elements

dom4j.Node to process xml files, and I had a problem with Map-similar structures.
I have followed document structure:
<Party>
<Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier>
<Address>
</Address>
<Contact>
<ContactInfo>
<Key>IPS</Key>
<Value>null</Value>
<Key>keyTwo</Key>
<Value>1234</Value>
(...)
</ContactInfo>
</Contact>
</Party>
And my goal is to get value for keyTwo element. How to get those element with flexible, non hardcoded way?
My first idea was something like that:
//parent node is father element
Node parentDocument = parentNode.selectSingleNode("ContactInfo");
List<Node> nodesKeys = contactNode.selectNodes("Key");
List<Node> nodesValues = contactNode.selectNodes("Value");
for(int i=0; i<nodesKeys.size(); i++){
if(nodesKeys.get(i).selectSingleNode("Key").equals("keyTwo")){
return nodesValues.get(i);
}
}
But I'm not sure if it is good approach, especially if list of keys and values will be correctly ordered.
Here is a complete working example:
Maven : PoM
<?xml version="1.0" encoding="UTF-8"?>
<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>com.sofrecom</groupId>
<artifactId>XmlProcessing</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
Java Main:
package xmlprocessing;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
*
* #author z.benrhouma
*/
public class Main {
public static void main(String... args) {
try {
Document document = parse("src/main/resources/file.xml");
Node node = document.selectSingleNode( "//Party/Contact/ContactInfo/Key[text()='keyTwo']/following-sibling::Value[1]");
System.out.println(node.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
public static Document parse(String path) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(path);
return document;
}
}
XML file :
<?xml version="1.0" encoding="UTF-8"?>
<Party>
<Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier>
<Address>
</Address>
<Contact>
<ContactInfo>
<Key>IPS</Key>
<Value>null</Value>
<Key>keyTwo</Key>
<Value>1234</Value>
</ContactInfo>
</Contact>
</Party>
I think dom4j+Xpath will resolve the problem , you just need to add jaxen dependency.
// ContactInfo Element having key = 'keyTwo'
Node node = document.selectSingleNode( "//Party/Contact/ContactInfo[Key = 'keyTwo']");
// retrieve data from the selected node
String value = node... ;
Maven :
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
Cheers.

Categories