I have a simple Maven Project with a single Java class that connects to a DB, runs a Query and outputs the result.
SingleQueryExecutor.java:
public class SingleQueryExecutor {
public static void main(String[] args) throws ClassNotFoundException {
String url = "jdbc:sqlserver://somehost:1433;DatabaseName=somedb;userName=someuser;password=somepassword;integratedSecurity=true;authenticationScheme=JavaKerberos;loginTimeout=10;socketTimeout=1000;applicationIntent=ReadOnly;sendStringParametersAsUnicode=false;queryTimeout=10;cancelQueryTimeout=5;encrypt=false";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("Connecting...");
try (Connection connection = DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement()) {
System.out.println("Executing query...");
String query = "SELECT count(*) FROM some_table WHERE some_column='some_value'";
ResultSet resultSet = statement.executeQuery(query);
System.out.println("Checking result...");
if (resultSet.next()) {
System.out.println("Count: " + resultSet.getInt(1));
}
System.exit(0); // Normal termination
} catch (SQLException e) {
e.printStackTrace();
System.exit(1); // Abnormal termination
}
}
The program requires a Kerberos config file to connect to our DB. I’m able to successfully compile:
javac SingleQueryExecutor.java
and successfully run via:
java -cp mssql-jdbc-12.6.1.jre11.jar:. SingleQueryExecutor
This will pick up my environment variable _JAVA_OPTIONS=-Djava.security.krb5.conf=/opt/kevin/krb5/krb5.conf
and successfully run.
But it runs slow so I want to use GraalVM to compile the Program into a Native Image.
I’m using the Graal Maven Plugin to do so. The full native-image
command generated from the maven plugin looks like:
/home/runner/.local/graalvm-jdk-17_linux-x64/bin/native-image -cp /home/runner/work/SingleQueryExecutor/target/SingleQueryExecutor-1.0-SNAPSHOT.jar:/home/runner/.m2/repository/com/microsoft/sqlserver/mssql-jdbc/12.6.1.jre11/mssql-jdbc-12.6.1.jre11.jar --no-fallback -H:Path=/home/runner/work/SingleQueryExecutor/target -H:Name=singlequeryexecutor -H:Class=org.example.SingleQueryExecutor -H:DashboardDump=singlequeryexecutor -H:AdditionalSecurityProviders=sun.security.jgss.SunProvider -H:+DashboardAll -Djava.security.krb5.conf=/opt/kevin/krb5/krb5.conf -H:ResourceConfigurationFiles=/home/runner/work/SingleQueryExecutor/src/main/resources/META-INF/native-images/resource-config.json -H:ReflectionConfigurationFiles=/home/runner/work/SingleQueryExecutor/src/main/resources/META-INF/native-images/reflect-config.json
The key piece for this question being -Djava.security.krb5.conf=/opt/kevin/krb5/krb5.conf
. Because when I run the binary, it’s ignoring this Java Property and instead just looking at the default krb5.conf file (/etc/krb5/krb5.conf).
I have tried setting the KRB5_CONFIG
but no luck.
I have tried include -Djava.security.krb5.conf=/opt/kevin/krb5/krb5.conf
when I execute the binary but no luck.
I see there is a <jvmArgs>
config possible, but without an example, I wasn’t able to get it to work.
How do I get GraalVM to build a native-image and be able to include Java Properties?