Search code examples
javaandroidbashasn.1asn1tools

ASN1 Compile issue with Bash file in Android


I am trying to generate java files from asn1 file using the below in my android project and facing error in generating the files in Mac OS and in window it worked fine.

org.openmuc:jasn1:1.9.0
org.openmuc:jasn1-compiler:1.9.0

Below is the bash file

#!/bin/bash

JARS_LOCATION="../build/libs-all"
MAIN_CLASS="org.openmuc.jasn1.compiler.Compiler"
SYSPROPS=""
PARAMS=""

SCRIPT_HOME=`dirname $0`

CLASSPATH=$(JARS=("$SCRIPT_HOME"/"$JARS_LOCATION"/*.jar); IFS=:; echo "${JARS[*]}")

for i in $@; do 
    if [[ $i == -D* ]]; then
        SYSPROPS="$SYSPROPS $i";
    else
        PARAMS="$PARAMS $i";
    fi
done

java $SYSPROPS -cp $CLASSPATH $MAIN_CLASS $PARAMS

and the above bash file will be called from my projects build.gradle file. Below is the final command will be executed from the bash. Shortened the file paths for easy understanding.

java -cp "/libs/antlr-2.7.7.jar:/libs/jasn1-1.9.0.jar:/libs/jasn1-compiler-1.9.0.jar" org.openmuc.jasn1.compiler.Compiler -p com.test.package.asn1 -f /asn1def/asn1/RSPDefinition.asn /asn1def/asn1/PKIX1Explicit88.asn /asn1def/asn1/PKIX1Implicit88.asn -o /asn1def/build/generated/source/java

Below is the error I am getting

Generated code will be saved in /asn1def/build/generated/source/java
    Parsing "/asn1def/asn1/RSPDefinition.asn"
    Parsing "/asn1def/asn1/PKIX1Explicit88.asn"
    Parsing "/asn1def/asn1/PKIX1Implicit88.asn"
    Generating classes for module "PKIX1Implicit88"
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
            at org.openmuc.jasn1.compiler.HexConverter.toShortHexString(HexConverter.java:63)
            at org.openmuc.jasn1.compiler.HexConverter.toShortHexString(HexConverter.java:59)
            at org.openmuc.jasn1.compiler.HexConverter.appendShortHexString(HexConverter.java:99)
            at org.openmuc.jasn1.compiler.HexConverter.appendHexString(HexConverter.java:108)
            at org.openmuc.jasn1.compiler.HexConverter.toHexString(HexConverter.java:9)
            at org.openmuc.jasn1.compiler.BerClassWriter.writeEncodeTag(BerClassWriter.java:2169)
            at org.openmuc.jasn1.compiler.BerClassWriter.writeSequenceOrSetEncodeFunction(BerClassWriter.java:1107)
            at org.openmuc.jasn1.compiler.BerClassWriter.writeSequenceOrSetClass(BerClassWriter.java:731)
            at org.openmuc.jasn1.compiler.BerClassWriter.writeConstructedTypeClass(BerClassWriter.java:493)
            at org.openmuc.jasn1.compiler.BerClassWriter.translateModule(BerClassWriter.java:194)
            at org.openmuc.jasn1.compiler.BerClassWriter.translate(BerClassWriter.java:133)
            at org.openmuc.jasn1.compiler.Compiler.main(Compiler.java:89)
    Caused by: java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
            at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
            at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
            ... 12 more
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':asn1def:compileAsn'.
    > Process 'command '/asn1def/run-scripts/jasn1-compiler'' finished with non-zero exit value 1

I have the main class in the jasn1-compiler-1.9.0.jar file in the libs folder. The same generate files in windows but in Mac it is not working. The difference in windows & Mac is Java version. Mac has Java 11 & windows has Java 1.8. I also tried adding the JDK 1.8 location as below at the end of above code.

-Dorg.gradle.java.home=/Users/Shared/Jenkins/.jenkins/tools/hudson.model.JDK/JDK_1_8

Can anyone help me to point out what's missing?


Solution

  • Finally I found the answer. The error was happened because of the Java version in Mac, it has Java 11 and it didn't support javax xml bind. So I installed the JDK 1.8 in the mac machine and use that path in the bash file as below before the java execution line in bash file.

    export JAVA_HOME=/usr/java/jdk1.8.161/
    

    It may useful for someone.