我正在用Java中的Playframework 2.2编写一个webapp。
现在我想添加一个小的独立文本到数据库导入工具,它只包含一个带有main
方法的Java文件:
public static void main(String[] args) {
importTextToDatabase();
}
activator
(或sbt
)(没有网络应用)中运行它?答案 0 :(得分:8)
几个月前想出来并忘记了。我花了两个小时再搞清楚。答案是run-main。诀窍是你需要围绕run-main的引号和它后面的参数,否则它会给你一个错误。所以......
如果您有一个类my.package.Main,您可以使用:
运行它 play "run-main my.package.Main"
我相信您也可以使用类似的命令直接从sbt运行它:
sbt "run-main my.package.Main"
答案 1 :(得分:3)
适用于Play 2.3
在stage和dist环境中,有一个启动脚本与bin
目录中的项目名称相同。在该脚本中,有一个app_mainclass
变量,指定脚本要执行的主类。您可以复制生成的脚本并替换主类变量的定义以运行您自己的 main 。这样,您可以使用脚本的其他内置功能将额外的参数传递给JVM,设置调试端口,并指定主程序参数。
答案 2 :(得分:1)
I had very similar problem pretty recently and it appears that Play Framework have this issue already addressed. It works perfectly fine with version 2.5.x. I am not sure if that was available in previous versions though.
Basically, execution of stage
command should generate Linux and DOS/Windows launcher scripts (in target/target/universal/stage/bin
directory). When you run that script with option -h
, you should get all execution parameters. One of them is:
-main <classname> Define a custom main class
So that way you can run standalone applications (e.g. ./target/universal/stage/bin/your-app -main com.your.Application), which have access to the same libraries or configuration as your Play web server.