简体   繁体   English

如何在 grails 中运行 Java 程序

[英]how to run a java program in grails

I am so confused on this .... I have a Java application that I have written and want to run it in grails.我对此感到很困惑......我有一个我编写的 Java 应用程序,并希望在 grails 中运行它。

I found this article ( How to run java programs in grails? ) and I am confused about the solution that was approved.我找到了这篇文章( 如何在 grails 中运行 java 程序? ),但我对获得批准的解决方案感到困惑。

  1. Do i need to make a controller first?我需要先制作一个控制器吗?
  2. Do i have to import my java class files from my java application in the controller?我是否必须从控制器中的 Java 应用程序导入我的 Java 类文件?
  3. do i create ANOTHER controller inside src/java and then call the second controller (inside src/java) from the controller in the /project_name/Controllers folder??我如何在 src/java 中创建另一个控制器,然后从 /project_name/Controllers 文件夹中的控制器调用第二个控制器(在 src/java 中)?

I just need some clarity.我只需要一些澄清。

Thanks!谢谢!

UPDATE:更新:

Okay -- I made my project and then put in a contraller and put my java files in src/jave.好的 - 我制作了我的项目,然后放入一个控制器并将我的 java 文件放在 src/jave 中。 I have one controller file and this is what it is (and this is the code in my controller):我有一个控制器文件,这就是它的内容(这是我的控制器中的代码):

//package mttestbox
import mttestbox.MTBoxController
//import MTInit

class MTBoxController {

    def index() 
    { 
        MTInit.main(RunMT_GUI)
    }
}

I am getting the following error (and it is not a surprise because I am so confused):我收到以下错误(这并不奇怪,因为我很困惑):

URI:/MTTestBox/MTBox/index
Class: groovy.lang.MissingPropertyException
Message: No such property: MTInit for class: mttestbox.MTBoxController

What did I do wrong???我做错了什么???

Thanks!!谢谢!!

Agree with everything said so far but want to mention, without being certain what the OP's true intent is, that grails jobs might be what you want.同意到目前为止所说的一切,但想提一下,在不确定 OP 的真正意图是什么的情况下,grails 工作可能就是你想要的。 They're processes that are run on a timer using Quartz, so if you're just looking to run something on the server periodically, without any web interaction, you could go that route.它们是使用 Quartz 在计时器上运行的进程,因此如果您只是希望定期在服务器上运行某些内容,而无需任何 Web 交互,您可以走这条路。

First, Grails is used to create web based applications, so you will need a controller that will respond to your browser request.首先,Grails 用于创建基于 Web 的应用程序,因此您需要一个控制器来响应您的浏览器请求。

Secondly, if your controller is in a different package than the class of your Java program that has your public static void main(String[] args) method then you will need to import that class into your controller.其次,如果您的控制器与具有public static void main(String[] args)方法的 Java 程序的类位于不同的包中,那么您需要将该类导入到您的控制器中。

Finally, you do not need to create another controller inside src/java .最后,您不需要在src/java创建另一个控制器。 Controllers exist within the grails-app/controllers directory structure.控制器存在于grails-app/controllers目录结构中。

Following the instructions provided in the linked post in your question should give you all you need to accomplish calling your Java program from a Grails controller.按照问题中链接帖子中提供的说明进行操作,应该可以为您提供从 Grails 控制器调用 Java 程序所需的一切。

I found the answer.我找到了答案。 I found out that you have to put the groovy controller package declaration in the java class files(s).我发现您必须将 groovy 控制器包声明放在 java 类文件中。

package <groovy controller name>;

once that is done, the import can then be used in the controller.完成后,可以在控制器中使用导入。 Once i did this, i was able to fire up grails and goto the default page and then, my app ran on my machine as an executable from a web call in grails (i did a /WEB-INF/grails-app/views/XXX/index.gsp not found error, but I think that because I have not made any GSPs yet (???).一旦我这样做了,我就能够启动 grails 并转到默认页面,然后,我的应用程序作为来自 grails 网络调用的可执行文件在我的机器上运行(我做了一个 /WEB-INF/grails-app/views/ XXX/index.gsp not found 错误,但我认为这是因为我还没有制作任何 GSP (???)。

Here is neat trick (found this using NetBeans): if you have more than one Java class file: 1. edit one of them by adding the groovy controller package name.这是一个巧妙的技巧(使用 NetBeans 发现的):如果您有多个 Java 类文件: 1. 通过添加 groovy 控制器包名称来编辑其中一个。 2. Save jave file. 2.保存jave文件。 3. Highlight all java class files needed (including the one that was edited in step 1). 3. 突出显示所有需要的 java 类文件(包括在步骤 1 中编辑的那个)。 4. move them to Java Source Packages folder in grails project. 4. 将它们移动到 grails 项目中的 Java Source Packages 文件夹。 5. when promted tell the IDE to move them. 5. 提示时告诉 IDE 移动它们。 6. When promted tell the IDE to Refactor. 6. 提示时告诉 IDE 进行重构。

Makes all the edits in all the needed files for you.为您在所有需要的文件中进行所有编辑。 Then - run and see what happens!!!然后——跑,看看会发生什么!!!

Thanks everyone!!!谢谢大家!!!

ironmantis7x铁螳螂7x

In grails 3 you have to put your java program in src/main/groovy在 grails 3 中你必须把你的 java 程序放在 src/main/groovy

create a package "hello" inside src/main/groovy在 src/main/groovy 中创建一个包“hello”

package hello;

class HelloWorld {
      public static void main(String arg[]){ 

      System.out.println("running java program in grails 3");

      }
}

Now you just have to import hello.* in the controller and call the method inside index() method现在你只需要在控制器中导入 hello.* 并调用 index() 方法中的方法

def index(){
HelloWorld.main()
}

This should work.这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM