How to disable exception by default?

68 views
Skip to first unread message

Jiliang Li

unread,
May 26, 2021, 3:24:30 AM5/26/21
to javacpp
Hello,

we got two questions about using javacpp-presets

1. Is it possible to disable cpp exception by default?
We follow Google style, and do not use exception in C++ code. Adding NoException annotation to every function is tedious.

2. How to generate presets in local java packages instead of org.bytedeco.xxx?
As the c++ library is internal.

Thanks in advance.

Samuel Audet

unread,
May 26, 2021, 3:30:39 AM5/26/21
to javacpp...@googlegroups.com, Jiliang Li
We can annotate top classes with @NoException as well like this:
https://github.com/bytedeco/javacpp-presets/blob/master/tvm/src/main/java/org/bytedeco/tvm/presets/tvm.java#L61

The classes get generated in the package specified by
@Properties(target=...) like this:
https://github.com/bytedeco/javacpp-presets/blob/master/tvm/src/main/java/org/bytedeco/tvm/presets/tvm_runtime.java#L93

Jiliang Li

unread,
May 27, 2021, 8:31:32 AM5/27/21
to javacpp
Hi Samuel, NoException does not seem to work.

@Properties(
target = "xxx",
...
)
//@NoException
public class xxx implements InfoMapper {

Comment NoException or not, the generated jnixxx.cpp still contains try/catch

    try {
        int rval = ptr->xxx((const signed char*)ptr0, (size_t)arg1, adapter2);
        rarg = (jint)rval;
    } catch (...) {
        exc = JavaCPP_handleException(env, 8);
    }

Jiliang Li

unread,
May 27, 2021, 12:47:40 PM5/27/21
to javacpp
Hi Samuel, I have some questions regarding your solution to second problem.

Changing target and global works for generated java classes.  And classOrPackageNames is also need changed to make jni cpp files generated and compiled. I use the following command because modifying myproject/pom.xml does not easily work for me.

mvn install  -Djavacpp.deleteJniFiles=false -Djavacpp.classOrPackageNames="com.mycompany.myproject.*" --projects myproject

However, the generated myproject-macosx-x86_64.jar is missing some dynamic libraries comparing to initial generated jar under org.bytedeco group. 

Is there anything wrong with my steps?


myproject/pom.xml is as below.

<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp-presets</artifactId>
        <version>1.5.6-SNAPSHOT</version>
    </parent>

    <groupId>org.bytedeco</groupId>
    <artifactId>myproject</artifactId>
    <version>${revision}</version>
    <name>JavaCPP Presets for myproject</name>

    <properties>
        <revision>2.4.1-${project.parent.version}</revision>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.bytedeco</groupId>
                <artifactId>javacpp</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.moditect</groupId>
                <artifactId>moditect-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>mingw</id>
            <activation>
                <os>
                    <family>windows</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.bytedeco</groupId>
                        <artifactId>javacpp</artifactId>
                        <configuration>
                            <properties>${javacpp.platform}-mingw</properties>
                            <compilerOptions>
                                <compilerOption>-static-libgcc</compilerOption>
                                <compilerOption>-static-libstdc++</compilerOption>
                                <compilerOption>-Wl,-Bstatic</compilerOption>
                                <compilerOption>-lstdc++</compilerOption>
                                <compilerOption>-lgcc</compilerOption>
                                <compilerOption>-lgcc_eh</compilerOption>
                                <compilerOption>-lpthread</compilerOption>
                                <compilerOption>-Wl,-Bdynamic</compilerOption>
                            </compilerOptions>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>



在2021年5月26日星期三 UTC+8 下午3:30:39<Samuel Audet> 写道:

Samuel Audet

unread,
May 27, 2021, 10:07:19 PM5/27/21
to javacpp...@googlegroups.com, Jiliang Li
That works fine for me, as well as for other presets such as CUDA, MKL,
FFmpeg, etc. Could you provide a full example that fails? You may be
hitting some bug somewhere, but unless you're able to show something
that doesn't work, I won't be able to fix what isn't broken.

Samuel Audet

unread,
May 27, 2021, 10:14:20 PM5/27/21
to javacpp...@googlegroups.com, Jiliang Li
Since you're using the parent pom.xml file for the javacpp-presets,
you'll also need to override the lines where "org/bytedeco" and
"org.bytedeco" are written:
https://github.com/bytedeco/javacpp-presets/blob/master/pom.xml

BTW, this is more easily done with Gradle and Gradle JavaCPP:
https://github.com/bytedeco/gradle-javacpp/tree/master/samples/zlib

Mats Larsen

unread,
May 28, 2021, 12:07:16 AM5/28/21
to javacpp...@googlegroups.com
Can confirm that @NoException does not seem to work as intended. Planned to add it to the LLVM/Clang preset based on this discussion, but JavaCPP still generates code that wraps everything in a try-catch clause.

--
You received this message because you are subscribed to the Google Groups "javacpp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacpp-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/javacpp-project/2f4f6359-6a76-4f59-ac95-d15edb2188e8n%40googlegroups.com.

Samuel Audet

unread,
May 28, 2021, 12:28:26 AM5/28/21
to javacpp...@googlegroups.com, Mats Larsen
I've just tried it with LLVM and the patch below, and it's working fine
for me. The `try` and `catch` blocks of function calls all go away. I
see the ones for callbacks remain, I guess they probably shouldn't
though. If that's what you're referring to, please open an issue about it!

In any case, like I keep telling you guys, you'll need to provide more
details, such as explaining exactly what you're doing and the results
that you're getting vs what you're expecting.


diff --git a/llvm/src/main/java/org/bytedeco/llvm/presets/LLVM.java
b/llvm/src/main/java/org/bytedeco/llvm/presets/LLVM.java
index 263359ac44..b0ea261580 100644
--- a/llvm/src/main/java/org/bytedeco/llvm/presets/LLVM.java
+++ b/llvm/src/main/java/org/bytedeco/llvm/presets/LLVM.java
@@ -41,6 +41,7 @@ import org.bytedeco.javacpp.tools.*;
compiler = "cpp14", link = {"LLVM-12", "LTO@.12", "Remarks@.12"},
resource = {"include", "lib", "libexec", "share"}),
@Platform(value = "macosx", link = {"LLVM", "LTO", "Remarks"}),
@Platform(value = "windows", link = {"LLVM", "LTO", "Remarks"})})
+@NoException
public class LLVM implements InfoMapper {
static { Loader.checkVersion("org.bytedeco", "llvm"); }

Jiliang Li

unread,
May 28, 2021, 2:32:37 AM5/28/21
to javacpp
I tried with fftw and NoException works.

Maybe there is something wrong or some weird corner case with my project. I will try more and let you known if I have any progress or insight.

Mats Larsen

unread,
May 28, 2021, 10:59:51 AM5/28/21
to Samuel Audet, javacpp...@googlegroups.com
Apologies, I was in a bit of a hurry when I sent that last night. I used the diff as you did and I
have the same results as you have with the try-catch blocks being generated for callbacks.

I'll open up an issue in the JavaCPP repository addressing this, and submit open a PR for NoException
in the LLVM preset.

Jiliang Li

unread,
May 31, 2021, 3:46:38 AM5/31/21
to javacpp
Hi Samuel, I try to add per-class NoException annotation instead of per-function

```
infoMap.put(new Info("namespace::Someclass").annotations("@NoException"));
```

got the following errors

```
annotation type not applicable to this kind of declaration
```

the generated code looks like this

```
public native int do(@NoException @SharedPtr Someclass variable);
```

It seems it's illegal to annotate class. 
Is NoException only supposed to be used with function or top classes?

Thanks


在2021年5月26日星期三 UTC+8 下午3:30:39<Samuel Audet> 写道:

Samuel Audet

unread,
May 31, 2021, 5:41:42 AM5/31/21
to javacpp...@googlegroups.com
That's obviously a bug, so please report as an issue here:
https://github.com/bytedeco/javacpp/issues

Jiliang Li

unread,
May 31, 2021, 11:32:28 AM5/31/21
to javacpp
Reply all
Reply to author
Forward
0 new messages