bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.48k stars 581 forks source link

IntelliJ Fails To Compile #308

Closed vineetdodd closed 5 years ago

vineetdodd commented 5 years ago

Hello, I seem to be having a strange linking error with my IntelliJ Javacpp project.

I am using javacpp version 1.5 and I have configured an Application Configuration where I have set the following.

I am following the maven file structure, and I have my Java file underneath -> src/main/java/com/myproject/javatocpp/javacppexamples/JavaToCPP.java And my cpp file is under -> src/main/java/com/myproject/javatocpp/javacppexamples/JavaToCPP.cpp

Yet, I still keep getting the following error when I run this configuration.

java.lang.UnsatisfiedLinkError: no jniJavaToCPP in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1336) at org.bytedeco.javacpp.Loader.load(Loader.java:1077) at org.bytedeco.javacpp.Loader.load(Loader.java:947) at com.myproject.javatocpp.javacppexamples.JavaToCPP.<clinit>(JavaToCPP.java:11) Exception in thread "main"

From the docs, I was under the impression that Javacpp would call JNI by itself and take care of linking any dll files by itself, but it seems as though I need to set something else.

I saw old issues like this, but from the docs, they all seemed to be resolved. I have tried those solutions already, but I can't seem to get anywhere with those. I am not sure what to do, so I thought it would be best to submit an issue and see where it goes, just in case.

If there is any more information I would need to provide, please let me know. Thanks!

saudet commented 5 years ago

We still need to call the Builder before it can run. I don't think anyone has tried with IntelliJ IDEA without Gradle, but we just need to call a couple of commands during the build as given on this page: https://github.com/bytedeco/javacpp/wiki/Mapping-Recipes

vineetdodd commented 5 years ago

With regards to Maven, I thought that since the Maven dependency was included in the official documentation page, it seemed as though Maven was tested and supported. I would prefer to use Maven for my future projects, so I was wondering if you had any ideas regarding that issue specifically. What could be causing this linking error? I thought this was supposed to be handled directly? Do I need to configure something on my Java path.

Also I tried those commands, I am able to compile successfully, but it does not seem to be able to find the class during runtime. I wanted to see if it failed at a specific portion, but it does not even print those print statements, meaning that it does find the class entirely.

Exception in thread "main" java.lang.NoClassDefFoundError: JavaToCPP (wrong name: com/myproject/javatocpp/JavaToCPP) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at org.bytedeco.javacpp.tools.UserClassLoader.findClass(UserClassLoader.java:72) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.bytedeco.javacpp.tools.ClassScanner.addClass(ClassScanner.java:61) at org.bytedeco.javacpp.tools.ClassScanner.addClassOrPackage(ClassScanner.java:129) at org.bytedeco.javacpp.tools.Builder.classesOrPackages(Builder.java:890) at org.bytedeco.javacpp.tools.Builder.main(Builder.java:1308)

Thank you so much, I really think this framework would be great for what I am working on and I really do appreciate any help!

saudet commented 5 years ago

If you're using Maven, there's a sample project for that here, for example: https://github.com/bytedeco/sample-projects/tree/master/javacpp-mvn-simple-demo But it doesn't contain an execution for the parser, so that needs to be "enhanced". There is the pom.xml file from the presets that we can use as reference though: https://github.com/bytedeco/javacpp-presets/blob/master/pom.xml Please let me know if you have any questions about it!

vineetdodd commented 5 years ago

Thanks for the update! I really do appreciate it.

I am getting an error now saying that

Failed to execute goal org.bytedeco:javacpp:1.0:build (process-classes) on project javacpp-mvn-simple-demo: Failed to execute JavaCPP Builder: Cannot run program "cl": CreateProcess error=2, The system cannot find the file specified -> [Help 1]

I saw on the sample project issues that someone else was having the same problem, and you pointed out MSVC might be the problem. (https://github.com/bytedeco/javacpp/issues/33) Although I'm sure I have it installed in my case. Are there any other causes for this issue? Thanks!

saudet commented 5 years ago

You'll need to run your build inside this: https://docs.microsoft.com/en-us/cpp/build/walkthrough-compiling-a-native-cpp-program-on-the-command-line

vineetdodd commented 5 years ago

It compiles now, thanks!

However, following the instructions on (https://github.com/bytedeco/sample-projects/tree/master/javacpp-mvn-simple-demo) I seem to be getting an issue with the execution. Specifically, I get an error during the run command saying ->

java.lang.UnsatisfiedLinkError: no jniMultiplyDemo in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:550) at org.bytedeco.javacpp.Loader.load(Loader.java:415) at org.bytedeco.javacpp.Loader.load(Loader.java:358) at somepackage.MultiplyDemo.<clinit>(MultiplyDemo.java:11) Exception in thread "main" [ERROR] Command execution failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166) at org.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:804) at org.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:751) at org.codehaus.mojo.exec.ExecMojo.execute(ExecMojo.java:313) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:107) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:195) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:140) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:314) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:151) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:445) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:168) at org.apache.maven.cli.MavenCli.main(MavenCli.java:132) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

My guess is that it can't find the dll file (I'm using Windows), and so I need to do some kind of additional update. Any ideas as to why this might be happening? Any help is appreciated. In other words, I seem to be back at square one with this, even after trying with this example.

Thanks!

saudet commented 5 years ago

The DLL file is missing from the resources, yes. Where is it? Or is the issue that it's not getting created? If it's the latter, what is the error message you get when you try to create it?

vineetdodd commented 5 years ago

I don't know, I assumed that JavaCPP would create the dll file by itself and then reference it as necessary, and resolve anything it needed to along the way by keeping track of where it was. So, I do not go out of the two maven commands specified in the link of my last comment. As for whether a file named "jniMultiplyDemo" is there, it is not; I checked my directories. So I was wondering what actually happens and why the dll file is not being created? What exactly is the process JavaCPP follows? Thanks!

saudet commented 5 years ago

Well, start by trying the small examples from the command line, and then when that works, try to create a Maven project.

vineetdodd commented 5 years ago
  1. It works fine on Ubuntu, but not on Windows, so I see the command being used. Do you recommend trying that out (with appropriate file translation) first? I just thought I'd ask first, as I had tried playing with command lines first before I posted this.
  2. Can I add custom flags to the g++ compilation part? I did not see any files I could edit in the sample project to go back and add flags or include additional files or whatnot. If there is not direct way, do you have any work arounds? Any advice is appreciated, Thanks!!
saudet commented 5 years ago

If it works on Ubuntu, it will work the same on Windows in the command line window for MSVC: https://docs.microsoft.com/en-us/cpp/build/walkthrough-compiling-a-native-cpp-program-on-the-command-line

saudet commented 5 years ago

For a Maven projects, there is a property in the plugin to set custom compiler options: http://bytedeco.org/javacpp/apidocs/org/bytedeco/javacpp/tools/BuildMojo.html#compilerOptions

vineetdodd commented 5 years ago

I guess the question I should have asked originally was where is the .dll file stored on Windows? If that part could be broken down, I could keep track and see if the appropriate file is even being generated. If not, then I can go back and trace why the file is not being generated.

saudet commented 5 years ago

If you don't see a message containing its path with ".dll" in it, it's not getting generated.

vineetdodd commented 5 years ago

But where would it be stored if it were? I'm not able to see it in the project structure, so I was wondering if it is being stored elsewhere. Thanks!

saudet commented 5 years ago

By default it outputs in the same directory as the .class files. Try out a small sample on the command line and see for yourself!

vineetdodd commented 5 years ago

Hi, I was wondering if you could provide me with an example to use @Parameter(option = "javacpp.compileOption") in the documentation for http://bytedeco.org/javacpp/apidocs/org/bytedeco/javacpp/tools/BuildMojo.html#compilerOptions? I had found this as an example (https://github.com/bytedeco/javacpp/blob/master/src/main/java/org/bytedeco/javacpp/tools/BuildMojo.java), but I was not able to get it working. Instead of asking on a new thread, I thought I would just ask here. Any help is appreciated, thanks!

saudet commented 5 years ago

There are a few examples here: https://github.com/bytedeco/javacpp-presets/search?q=compilerOptions Let me know if that isn't enough

cuongnguyengit commented 1 year ago

javacpp is really not working in window. What a waste of time on this.

[INFO] [INFO] --- javacpp:1.4.1:build (process-classes) @ JavaCppTemplate --- [INFO] Detected platform "windows-x86" [INFO] Building for platform "windows-x86" [INFO] Generating javacpp\JavaCppTemplate\target\classes\jnijavacpp.cpp [INFO] Generating javacpp\JavaCppTemplate\target\classes\pl\krzysg\cppLib\target\jniMyCppLibConfig.cpp [INFO] Compiling javacpp\JavaCppTemplate\target\classes\pl\krzysg\cppLib\target\windows-x86\jniMyCppLibConfig.dll [INFO] cl /IE:\Project\javacpp\JavaCppTemplate/src/main/cpp/pl/krzysg/ "/IC:\Program Files (x86)\Java\jdk1.8.0_202\include" "/IC:\Program Files (x86)\Java\jdk1.8.0_202\include\win32" E:\Project\javacpp\JavaCppTemplate\target\classes\pl\krzysg\cppLib\target\jniMyCppLibConfig.cpp E:\Project\javacpp\JavaCppTemplate\target\classes\jnijavacpp.cpp /Oi /O2 /EHsc /Gy /GL /MD /LD -std=c++14 /W3 /link /OUT:jniMyCppLibConfig.dll psapi.lib Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27049 for x86 Copyright (C) Microsoft Corporation. All rights reserved. cl : Command line warning D9002 : ignoring unknown option '-std=c++14' jniMyCppLibConfig.cpp C:\Program Files (x86)\Java\jdk1.8.0_202\include\jni.h(39): fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory jnijavacpp.cpp C:\Program Files (x86)\Java\jdk1.8.0_202\include\jni.h(39): fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory