Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails:集成Java类_Java_Grails - Fatal编程技术网

Grails:集成Java类

Grails:集成Java类,java,grails,Java,Grails,我在IntelliJ IDE中创建了一个新的Grails 3应用程序 以下是IntelliJ的文件夹结构视图 然后我创建了两个文件,一个控制器,代码如下 package grailsjavatest import test.JavaTest; class JavaTestController { def index() { JavaTest jt = new JavaTest(); render jt.someAction(); } } 以及

我在IntelliJ IDE中创建了一个新的Grails 3应用程序

以下是IntelliJ的文件夹结构视图

然后我创建了两个文件,一个控制器,代码如下

package grailsjavatest
import test.JavaTest;
class JavaTestController {

    def index() {
        JavaTest jt = new JavaTest();
        render jt.someAction();
    }
}
以及包含以下代码的Java文件(在src/Java/test/JavaTest.Java中)

如果我将控制器代码更改为

package grailsjavatest
class JavaTestController {

    def index() {
        render "<html> <body> <b> Hello From Java! </b> </body> </html>";
    }
}
包grailsjavatest
类JavaTestController{
def索引(){
呈现“Hello From Java!”;
}
}
页面按预期呈现(但显然不调用java代码)

编辑

这是创建包的文件结构


我以前的回答没有什么意义


问题在于,“test”不是一个包文件夹(在IntelliJ中包含一个点),而是一个普通文件夹。但为了向Java文件夹添加包文件夹,需要将Java文件夹标记为“源文件夹”(蓝色)。右键单击java文件夹,选择“将目录标记为”-“源根目录”。“test”文件夹现在将自动标记为包文件夹(至少在IntelliJ 2016.1中是这样)。

您的JavaTest类是使用默认(包专用)修饰符声明的,因此它仅对同一“test”包中声明的其他类可见。在类定义的开头添加'public'关键字(即'public class JavaTest{yada,yada')


或者按照Grails给出的说明和“使用--stacktrace选项运行以获取堆栈跟踪”,这应该会告诉您出了什么问题。

遗憾的是,这不起作用,即使将src标记为源根,并将java标记为包,也会发生相同的错误。
[the java process command was removed as it is a bit long.]

| Running application...
startup failed:

/Users/user/IdeaProjects/GrailsJavaTest/grails-app/controllers/grailsjavatest/JavaTestController.groovy: 2: unable to resolve class test.JavaTest
 @ line 2, column 1.
   import test.JavaTest;
   ^

1 error



FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileGroovy'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
| Error Failed to start server (Use --stacktrace to see the full trace)

Process finished with exit code 1
package grailsjavatest
class JavaTestController {

    def index() {
        render "<html> <body> <b> Hello From Java! </b> </body> </html>";
    }
}