problems running state machine examples

540 views Asked by At

Congratulations on the spring state machine, I found it yesterday and have been trying it out, specifically the turnstile example running in STS. I found it very easy and intuitive to build a FSM.

Because spring shell doesn't work well in STS I tracked down the instructions to run the examples from the command line in the reference doc,

"java -jar spring-statemachine-samples-turnstile-1.0.0.BUILD-SNAPSHOT.jar"

, but running it got an error

"no main manifest attribute, in spring-statemachine-samples-turnstile-1.0.0.BUILD-SNAPSHOT.jar".

Although not even a novice in using gradle, I tried fixing this by adding this line to build.gradle in the jar section

"manifest.attributes['Main-Class'] = 'demo.turnstile.Application'" (which doesn't handle the various sub-projects I know) but got this error "NoClassDefFoundError: org/springframework/shell/Bootstrap".

If it is possible to run the samples from gradle, could you include them in the reference document? I tried running the samples using

gradle run but it there was no interaction with the shell scripts.

2

There are 2 answers

1
Janne Valkealahti On

Samples are designed to be run as executable jar and with shell so that you can interact without a need to recompile with every change. Your error indicates that you didn't build that sample jar as mentioned in docs.

./gradlew clean build -x test

This will automatically use spring boot plugin which will add the necessary jar manifest headers to jar meta info to make it a true executable jar. Essentially every every sample is a spring boot app.

1
Abhijeet On

Building SM sample projects in Windows Environment:

  1. Open Command prompt (windows key + r -->cmd-->Enter), Change directory to project root folder spring-statemachine-master (Inside the Extracted folder).

  2. Run gradlew install to get all spring dependencies copied to local machine.

  3. Run gradlew clean build -x test to get the spring shell jars built. Courtesy Janne

These steps should ideally get all .jar built, look into \build\libs folder of respective sample project for jar files.

Run the like any other java jar file java -jar [jar-file-name.jar] (make sure to be change directory to jar file directory location).

One more thing where I was stuck was, How to give events to SM: It's like this sm event EVENT_NAME_AS_DEFINED_IN_CLASS. Ref E.g.: sm event RINSE --> to washer project