Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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
Java spring启动后启动opencv时出错_Java_Spring_Opencv_Spring Boot_Spring Tool Suite - Fatal编程技术网

Java spring启动后启动opencv时出错

Java spring启动后启动opencv时出错,java,spring,opencv,spring-boot,spring-tool-suite,Java,Spring,Opencv,Spring Boot,Spring Tool Suite,我正在使用spring工具套件创建一个名为opencv的spring引导后端。 我用示例代码创建了一个类来测试opencv,如下所示: @Service public class opencvTests { @PostConstruct public static void mymethod(){ System.out.println("Welcome to OpenCV " + Core.VERSION); System.out.println(

我正在使用spring工具套件创建一个名为opencv的spring引导后端。 我用示例代码创建了一个类来测试opencv,如下所示:

@Service
public class opencvTests {
    @PostConstruct
    public static void mymethod(){
        System.out.println("Welcome to OpenCV " + Core.VERSION);
        System.out.println(System.getProperty("java.library.path"));
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat m  = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("m = " + m.dump());

        return;
    }
}
当我在我的应用程序的主方法中直接调用该方法时,它工作得很好,但会阻止spring引导正确启动

@SpringBootApplication
public class OpencvTestingApplication {

    public static void main(String[] args) {
        opencvTests.mymethod()
        SpringApplication.run(OpencvTestingApplication.class, args);
    }
}
因此,我使用@PostConstruct注释在spring boot运行时启动该方法,但是。。。它在矩阵对象调用时失败,就像opencv库尚未加载一样。我尝试了几种替代方法,例如使用ApplicationReadyEvent,但问题仍然存在。(精度:opencv jar已经添加到项目中,opencv库路径作为参数添加到maven中)

对于opencv和spring boot,我还有很多需要学习的地方,不幸的是,我在stackoverflow或google上找不到任何有效的解决方案。 有人能告诉我一个有用的解决方法吗?我真的很感激,因为这个项目对我来说很重要


谢谢你的阅读

我真的没有答案:-(…但是,您是否尝试过在spring boot之外的“普通”java项目中使用opencv库?我的直觉是,首先尝试运行opencv库,而不必在spring boot中安装初始化逻辑。一旦您准确地了解了在工作状态下安装opencv库所需的步骤顺序在上,您可以返回到尝试将这些步骤融入
@PostConstruct
或其他类型的机制中,您可以使用这些机制在spring boot中的特定时间执行代码位。顺便说一句,仔细阅读示例代码,我看到您在启动spring boot应用程序之前,在main的开头调用了“mymethod”(使用
SpringApplication.run
)所以这可能就是问题所在。你在应用程序启动之前调用了你的方法,所以spring boot实际上还没有机会在你的应用程序中运行那些
@PostConstruct
方法。事实上,它根本没有机会做任何事情。谢谢你,克里斯,但当我在spring之后运行它时,这句话会被评论(否则@PostConstruct会被注释)。我选择此表单来显示这两种情况,因为问题是:如果我先运行opencv函数,Spring不会启动,或者如果我在之后启动它,则找不到opencv库路径:/
Welcome to OpenCV 3.2.0-dev
/home/*****/opencv/build/lib
2017-05-26 03:37:09.475  WARN 14951 --- [  restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'opencvTests': Invocation of init method failed; nested exception is java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_eye(III)J
2017-05-26 03:37:09.522  INFO 14951 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-05-26 03:37:09.559 ERROR 14951 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'opencvTests': Invocation of init method failed; nested exception is java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_eye(III)J
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at be.heh.opencvtesting.OpencvTestingApplication.main(OpencvTestingApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.3.RELEASE.jar:1.5.3.RELEASE]
Caused by: java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_eye(III)J
at org.opencv.core.Mat.n_eye(Native Method) ~[opencv-320.jar:na]
at org.opencv.core.Mat.eye(Mat.java:492) ~[opencv-320.jar:na]
at be.heh.opencvtesting.opencvTests.mymethod(opencvTests.java:18) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
... 22 common frames omitted