Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playframework app including a standalone main application

I am writing a webapp with Playframework 2.2 in Java. Now I want to add a small standalone text-to-database-import tool, which consists of only one Java file with a main method:

public static void main(String[] args) {
  importTextToDatabase();
} 
  • Can I include this standalone mini-app together with the rest of the webapp?
  • How can I then run it in the activator (or sbt) (without the webapp)?
  • Or is it better to create a second project for this app?
like image 943
Sonson123 Avatar asked May 26 '14 15:05

Sonson123


2 Answers

Figured this out a few months ago and forgot. Just took me two hours to figure it out again. The answer is run-main. The trick is you need quotes around run-main and the parameters that follow it or it will give you an error. So...

If you have a class my.package.Main your would run it with:

play "run-main my.package.Main"

I believe you can also run it directly from sbt with a similar command:

sbt "run-main my.package.Main"

Newer versions of sbt requires: sbt "runMain my.package.Main"

like image 110
cmorris Avatar answered Oct 01 '22 04:10

cmorris


Applicable for Play 2.3

In stage and dist environment, there is a startup script named the same as the project name in the bin directory. In that script, there is a app_mainclass variable specifying the main class to be executed by the script. You can copy the generated script and replace the definition of the main class variable to run your own main. With that, you can use the other built-in goodies of the script to pass extra parameters to the JVM, setup debug port, and specify your main program arguments.

like image 35
k.c. sham Avatar answered Oct 01 '22 06:10

k.c. sham