Friday, March 9, 2018

plant UML maven problem - Cannot run program "/usr/bin/dot": error=2

Recently I had problem to generate UML diagram by maven plugin 'plantuml-maven-plugin'. There was exception:

java.io.IOException: Cannot run program "/usr/bin/dot": error=2, No such file or directory
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
 at java.lang.Runtime.exec(Runtime.java:620)
 at net.sourceforge.plantuml.cucadiagram.dot.ProcessRunner$MainThread.startThreads(ProcessRunner.java:165)
 at net.sourceforge.plantuml.cucadiagram.dot.ProcessRunner$MainThread.runJob(ProcessRunner.java:125)
 at net.sourceforge.plantuml.api.TimeoutExecutor$MyThread.run(TimeoutExecutor.java:77)
Caused by: java.io.IOException: error=2, No such file or directory
 at java.lang.UNIXProcess.forkAndExec(Native Method)
 at java.lang.UNIXProcess.(UNIXProcess.java:247)
 at java.lang.ProcessImpl.start(ProcessImpl.java:134)
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
 ... 4 more
java.io.IOException: Cannot run program "/usr/bin/dot": error=2, No such file or directory
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
 at java.lang.Runtime.exec(Runtime.java:620)
 at net.sourceforge.plantuml.cucadiagram.dot.ProcessRunner$MainThread.startThreads(ProcessRunner.java:165)
 at net.sourceforge.plantuml.cucadiagram.dot.ProcessRunner$MainThread.runJob(ProcessRunner.java:125)
 at net.sourceforge.plantuml.api.TimeoutExecutor$MyThread.run(TimeoutExecutor.java:77)
Caused by: java.io.IOException: error=2, No such file or directory
 at java.lang.UNIXProcess.forkAndExec(Native Method)
 at java.lang.UNIXProcess.(UNIXProcess.java:247)
 at java.lang.ProcessImpl.start(ProcessImpl.java:134)
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
 ... 4 more

For me was surprising that sequence diagrams works fine without graphviz. Activity UML diagrams caused this exception. Solution is simple just install graphviz. At mac run:

brew install graphviz
brew link --overwrite graphviz
And it's done.

Sunday, February 11, 2018

How to use google fonts in JavaFX

There are some resources on internet but not all of them works for me. Following method works for me. Here is my step-by-step guide. Google fonts are at https://fonts.google.com. I wasn't able to download fonts directly from this site, I didn't found link to download. luckily there is a github repository where are all fonts available to download. It's at https://github.com/google/fonts/

Download font

Get font and download it to you project at src/main/resources/font/.

Make font available in JavaFX

Now I have to tell JavaFX something like look at there is new font called 'Cardo'. It could be done by static method loadFont on javafx.scene.text.Font class.

Here is a example:

Font f = Font.loadFont(Main.class.getResource("/font/Cardo-Regular.ttf").toExternalForm(), 12);

Use font in UI

Defined font could be used from css file and directly from java code.

from css

Font can be used from css file like this:

final Scene scene = new Scene();
scene.getStylesheets().add("gui/myStyle.css");
And than set all text in application to new font:
.root {
  -fx-font-family: Cardo;
}

From java FX code

Following code write to give Graphics object text black "Hello world":

graphics.setFont(f);
graphics.setTextAlign(TextAlignment.CENTER);
graphics.setTextBaseline(VPos.CENTER);
graphics.setFill(Color.BLACK);
graphics.fillText("Hello world", 100, 50);

Thursday, June 23, 2016

JMeter - how to call external groovy script

Recently was problem to call external script from JMeter. It's really useful to have shared library between different scrips in JMeter. Recently I have to create separate maven project producing jar which should be placed to JMeter. It was quite time expensive and difficult to maintain and fragile. Call external script from BeanShell script is possible but it's not easy.

Luckily in JMeter 3.0 is functionality JSR223. Now it's easy task.

  • Create configuration element User defined variable and create property 'scriptHome'.
    ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer   .getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
    
    From now in JMeter is available variable scriptHome. Variable point to directory where is places .jmx file.
  • In directory where is placed .jmx test file create file 'helloWorld.groovy' with content:
    public class Test{
        def sayHello(){
             println "Hello world"
        }
    }
    
  • Finally create JSR223 Sampler. As script language choose Groovy. Paste following script:
    def scriptHome = vars.get("scriptHome");
    def gcl = new GroovyClassLoader()
    def clazz2 = gcl.parseClass(new File(scriptHome + "helloWorld.groovy"))
    def o = clazz2.newInstance()
    o.sayHello()
    
Now run test, on console will be output 'Hello world'.

Asynchronous thread with timeout

Recently I have problem to start asynchronous process with timeout. More precisely I want main process to start new thread with some computation task. If task execution exceed some given time period than task is killed. I know how to do that in old 1.4 Java with thread instance for task in some thread group and some watchdog thread.

After some time I wrote following code:

@Test
public void scheduler() throws Exception {
 final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);

 FutureTask task = new FutureTask(() -> {
  try {
   System.out.println("task started");
   TimeUnit.SECONDS.sleep(10);
   System.out.println("task je dokoncen");
  } catch (InterruptedException e) {
   System.out.println("interrupted");
   return "task is done with interruption";
  }
  return "task is done";
 });

 scheduler.schedule(task, 100, TimeUnit.MILLISECONDS);

 scheduler.schedule(() -> {
  System.out.println("kill job is started");
  task.cancel(true);
  System.out.println("kill job is done");
  return "";
 } , 2, TimeUnit.SECONDS);

 System.out.println("I'm going to wait for ending both tasks");

 TimeUnit.SECONDS.sleep(20);
}

Friday, June 17, 2016

How to download latest snapshot from maven repository

Previously I had problem to download latest snapshot build from maven repository. It's not about copying file from maven repository home ~/.m2/repository/. It's just part of process. First it's necessary to download latest build from maven repository. Easiest way is to push maven to do it. For example let's have package com.example:billing:1.0.0-SNAPSHOT:war. Command will look like:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get --batch-mode -Dmdep.useBaseVersion=true -Dartifact=com.example:billing:1.0.0-SNAPSHOT:war -DremoteRepositories=example-snapshots::default::http://companynexus.com/nexus/content/repositories/snapshots/

Detail description can by found at http://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html.

Copy snapshot artifact from local repository can by done like this:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dmdep.useBaseVersion=true -DoutputDirectory=/opt/billing/ -Dartifact=com.example:billing:1.0.0-SNAPSHOT:war
Description can be found at http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html.

Saturday, December 5, 2015

JMH - ERROR: Unable to find the resource: /META-INF/BenchmarkList

I tried to use micro benchmarking tool called JMH. It look really nice. I used it for speed verification of my B+Tree implementation. It's at https://github.com/jajir/jblinktree. I'm using eclipse for building project and after some time I met following exception:

Exception in thread "main" java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList
	at org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:96)
	at org.openjdk.jmh.runner.BenchmarkList.find(BenchmarkList.java:104)
	at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:228)
	at org.openjdk.jmh.runner.Runner.run(Runner.java:178)
	at com.coroptis.jblinktree.performance.MapTestRunner.main(MapTestRunner.java:75)

Problem is that class BenchmarkList didn't find file '/META-INF/BenchmarkList'. This file contains list of precompiled benchmark tests. This file was missing because I compile project form eclipse and not from maven. Solution is compile project from command link:

mvn clean install

And from now it's work fine. If you have problem to configure JMH in maven look at https://github.com/jajir/jblinktree/blob/master/benchmark/pom.xml.

Sunday, August 9, 2015

How to install jpf-concurrent module

JPF is tool for static code analysis. I tried to use it for proving that code is thread safe. In multiple thread environment is problem with JVM 'state explosion'. When JPF try to examine all possible JVM states in multiple thread environment number of possible states grow exorbitantly. As documentation say this problem could be solved with module jpf-concurrent.

Because I didn't find guide how to install it here is simple guide.

Prerequisites

Installation steps

  • Go to directory where is jpf-core installed. In my case it's directory '/Users/jan/projects/jpf/'
  • Checkout project
    hg clone http://babelfish.arc.nasa.gov/hg/jpf/jpf-concurrent
  • Build jpf-concurrent project
    ant
  • Edit your site property file. In my case is at '/Users/jan/.jpf/site.properties'. Add there following section:
    # concurrent extension
    jpf-concurrent = ${jpf.home}/jpf-concurrent
    extensions+=,${jpf-concurrent}
    
Ant it's done. Good luck.

[EDIT] - After some time of using jpf-concurrent project I had following problem:

gov.nasa.jpf.jvm.NoUncaughtExceptionsProperty
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock.unlock(gov.nasa.jpf.concurrent.peers.JPF_java_util_concurrent_locks_ReentrantLock)
at com.coroptis.jblinktree.NodeLocks.unlockNode(NodeLocks.java:96)
at com.coroptis.jblinktree.NodeStoreImpl.unlockNode(NodeStoreImpl.java:59)

I contacted Nastaran Shafiei via google group, he is creator of jpf-concurrent. He told me, that jpf-concurrent module is outdated and no longer supported. So I removed jpf-concurrent module and path finder start work fine.

So, do NOT install jpf-concurrent module.