Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, June 29, 2022

java.desktop/sun.awt.FontConfiguration.getVersion with NullPointerException


 Ref: https://stackoverflow.com/questions/65509533/npe-from-sun-awt-fontconfiguration-getversion-using-apache-fop-jdk-11-on-linux-s

Error:

Caused by: java.lang.NullPointerException
        at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262)
        at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225)
        at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:107)
        at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:719)

Solution:

Turns out the issue was not that I didn't have access to the fonts, the Linux OS required me to install fontconfig via yum.

yum install fontconfig

I am not sure why Oracle JDK 11 plus the missing Linux utility was the issue since this works now in my production environment with Oracle JDK 1.8 and without fontconfig being installed.


Saturday, January 8, 2022

Camel XML to Java DSL convert utility


 https://github.com/jorgecastro05/camel-xml2dsl

Installing the script:

pip install camel_xml2dsl-0.0.x-py3-none-any.whl

Where x belongs to release version

Running the script

xml2dsl --xml xml_context_file.xml

Building the project (for developers)

Install dependencies

python3 -m pip install --upgrade build
python -m build

build and install

python -m build && pip install dist/camel_xml2dsl-0.0.1-py3-none-any.whl --force-reinstall



Tuesday, November 24, 2020

How to run .SQL script using JDBC?

 

A database script file is a file that contains multiple SQL quries separated from each other. Usually, these files have the .sql extention.

Running .sql script files in Java

You can execute .sql script files in Java using the runScript() method of the ScriptRunner class of Apache iBatis. To this method you need to pass a connection object.

Therefore to run a script file −

  • Register the MySQL JDBC Driver using the registerDriver() method of the DriverManager class.
  • Create a connection object to establish connection with the MySQL database using the getConnection() method.
  • Initialize the ScriptRunner class of the package org.apache.ibatis.jdbc.
  • Create a Reader object to read the script file.
  • Finally, execute the script using the runScript(reader) method.

Example

Let us create a script file with name sampleScript.sql copy the following contents init. This script creates a table with name cricketers_data in MySQL database an populates it with five records.

CREATE DATABASE exampleDB;
use exampleDB;
CREATE TABLE exampleDB.cricketers_data(
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255)
);
insert into cricketers_data values('Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
insert into cricketers_data values('Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
insert into cricketers_data values('Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
insert into cricketers_data values('Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
insert into cricketers_data values('Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
select * from mydatabase.cricketers_data;

Add the following maven dependency (for the jar file mybatis-3.4.1.jar) to your pom.xml file −

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.4.5</version>
</dependency>

Example

Following JDBC program executes the sampleScript.sql file.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.ibatis.jdbc.ScriptRunner;
public class RunningScripts {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/talakai_noppi";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Initialize the script runner
      ScriptRunner sr = new ScriptRunner(con);
      //Creating a reader object
      Reader reader = new BufferedReader(new FileReader("E:\\sampleScript.sql"));
      //Running the script
      sr.runScript(reader);
   }
}

Output

Connection established......
CREATE DATABASE exampleDB
use exampleDB
CREATE TABLE exampleDB.cricketers_data(
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255)
)
insert into cricketers_data values('Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India')
insert into cricketers_data values('Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica')
insert into cricketers_data values('Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka')
insert into cricketers_data values('Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India')
insert into cricketers_data values('Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India')
select * from mydatabase.cricketers_data
First_Name Last_Name Year_Of_Birth Place_Of_Birth Country
Shikhar Dhawan 1981-12-05 Delhi India
Jonathan Trott 1981-04-22 CapeTown SouthAfrica
Lumara Sangakkara 1977-10-27 Matale Srilanka
Virat Kohli 1988-11-05 Delhi India
Rohit Sharma 1987-04-30 Nagpur India
 Reference: https://www.tutorialspoint.com/how-to-run-sql-script-using-jdbc 

Monday, November 25, 2019

Splash screen in java application.

The splash screen can be displayed at application startup, before the Java Virtual Machine (JVM) starts. The splash screen is displayed as an undecorated window containing an image. You can use GIF, JPEG, or PNG files for the image. Animation is supported for the GIF format, while transparency is supported both for GIF and PNG. The window is positioned at the center of the screen. The position on multi-monitor systems is not specified. It is platform and implementation dependent. The splash screen window is closed automatically as soon as the first window is displayed by Swing/AWT (may be also closed manually using the Java API, see below).
If your application is packaged in a jar file, you can use the "SplashScreen-Image" option in a manifest file to show a splash screen. Place the image in the jar archive and specify the path in the option. The path should not have a leading slash.
For example, in the manifest.mf file:
 Manifest-Version: 1.0
 Main-Class: Test
 SplashScreen-Image: filename.gif
 
If the Java implementation provides the command-line interface and you run your application by using the command line or a shortcut, use the Java application launcher option to show a splash screen. The Oracle reference implementation allows you to specify the splash screen image location with the -splash: option.
For example:
 java -splash:filename.gif Test
 
The command line interface has higher precedence over the manifest setting.
The splash screen will be displayed as faithfully as possible to present the whole splash screen image given the limitations of the target platform and display.
It is implied that the specified image is presented on the screen "as is", i.e. preserving the exact color values as specified in the image file. Under certain circumstances, though, the presented image may differ, e.g. when applying color dithering to present a 32 bits per pixel (bpp) image on a 16 or 8 bpp screen. The native platform display configuration may also affect the colors of the displayed image (e.g. color profiles, etc.)
The SplashScreen class provides the API for controlling the splash screen. This class may be used to close the splash screen, change the splash screen image, get the splash screen native window position/size, and paint in the splash screen. It cannot be used to create the splash screen. You should use the options provided by the Java implementation for that.
This class cannot be instantiated. Only a single instance of this class can exist, and it may be obtained by using the getSplashScreen() static method. In case the splash screen has not been created at application startup via the command line or manifest file option, the getSplashScreen method returns null.

Install and use xorg-server on macOS via Homebrew

  The instructions to install and use xorg-server on macOS via Homebrew: Install Homebrew (if you haven't already): /bin/bash -c ...