Java – How to convert System.nanoTime to Seconds

We can just divide the nanoTime by 1_000_000_000, or use the TimeUnit.SECONDS.convert to convert it. Note 1 second = 1_000_000_000 nanosecond JavaExample.java package com.mkyong; import java.util.concurrent.TimeUnit; public class JavaExample { public static void main(String[] args) throws InterruptedException { long start = System.nanoTime(); Thread.sleep(5000); long end = System.nanoTime(); long elapsedTime = end – start; System.out.println(elapsedTime); // …

Read more

Python – How to convert String to float

In Python, we can use float() to convert String to float. num = "3.1415" print(num) print(type(num)) # str pi = float(num) # convert str to float print(pi) print(type(pi)) # float Output 3.1415 <class ‘str’> 3.1415 <class ‘float’> References Python docs – float()

Python – How to convert int to String

In Python, we can use str() to convert a int to String. num1 = 100 print(type(num1)) # <class ‘int’> num2 = str(num1) print(type(num2)) # <class ‘str’> Output <class ‘int’> <class ‘str’> References Python docs – str() Python – How to convert String to int

Python – How to convert String to int

In Python, we can use int() to convert a String to int. # String num1 = "88" # <class ‘str’> print(type(num1)) # int num2 = int(num1) # <class ‘int’> print(type(num2)) 1. Example A Python example to add two numbers. 1.1 Add two String directly. num1 = "1" num2 = "2" num3 = num1 + num2 …

Read more

Java – Count the number of items in a List

In Java, we can use List.size() to count the number of items in a List package com.mkyong.example; import java.util.Arrays; import java.util.List; public class JavaExample { public static void main(String[] args) { List<String> list = Arrays.asList("a", "b", "c"); System.out.println(list.size()); } } Output 3 References Java doc – List

Python – How to convert float to String

In Python, we can use str() to convert float to String. pi = 3.1415 print(type(pi)) # float piInString = str(pi) # float -> str print(type(piInString)) # str Output <class ‘float’> <class ‘str’> References Python docs – str() Python – How to convert int to String Python – How to convert String to float

How to check MySQL version

In the terminal, we can type mysql -V (uppercase V) to display the current MySQL version installed on the server. Terminal $ mysql -V mysql Ver 8.0.18 for Linux on x86_64 (MySQL Community Server – GPL) The above output show MySQL version 8.0.18 is installed on the server. Server System Variables Alternatively, we can connect …

Read more

Java Selection sort example

Selection sort is an in-place comparison sort. It loops and find the first smallest value, swaps it with the first element; loop and find the second smallest value again, swaps it with the second element, repeats third, fourth, fifth smallest values and swaps it, until everything is in correct order. P.S Selection sort is inefficient …

Read more

Java Bubble sort example

Bubble sort is the simplest sorting algorithm, it compares the first two elements, if the first is greater than the second, swaps them, continues doing (compares and swaps) for the next pair of adjacent elements. It then starts again with the first two elements, compares, swaps until no more swaps are required. 1. Explanation #unsorted …

Read more

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

Try to log into the MySQL server, but it prompts the following error message: Terminal $ mysql ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO) 1. Solution – (using password: NO) The user ‘root’ need a password to log into the MySQL server. To solve it, please provide a password with -p …

Read more

Nginx + WordPress ERR_TOO_MANY_REDIRECTS

Fresh installs a WordPress on Windows for development, and hits the ERR_TOO_MANY_REDIRECTS error message? Tested URL : http://localhost/index.php/wp-admin/install.php Tested : PHP 7.1.10 WordPress 4.8.3 Nginx 1.12.1 MySQL 5.7.17 Windows 10 your-nginx\conf\nginx.conf upstream php { server 127.0.0.1:9999; } server { listen 80; server_name localhost; root www/wordpress; location / { try_files $uri $uri/ /index.php?$args; } location ~ …

Read more

PHP + Windows – Call to undefined function mysql_connect()

Install WordPress and hits the following error message : Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\nginx-1.12.1\www\wordpress\wp-includes\wp-db.php… Tested PHP 7.1.10 WordPress 4.8.3 Nginx 1.12.1 MySQL 5.7.17 Windows 10 Solution To install WordPress, PHP needs Mysql extension, to fix this, loads php_mysqli.dll in php.ini file. php.ini ; Directory in which the loadable extensions …

Read more

Nginx + PHP on Windows

This article shows you how to install and integrate Nginx and PHP on Windows. Tested Nginx 1.12.1 PHP 7.1.10 Windows 10 1. Install Nginx + PHP Basically, just download zip file and extracts it, no installation. To install Nginx Visit http://nginx.org/en/download.html Download nginx/Windows-1.12.2 Extract to C:\nginx-1.12.1 To Install PHP Visit http://windows.php.net/download/ Download PHP7.1 download VC14 …

Read more

php-cgi.exe – The application was unable to start correctly

Run php-cgi.exe and hits a pop up dialog showing the following error message : Application Error : The application was unable to start correctly (0xc000007b) Terminal c:\php7.1>php-cgi.exe -b 127.0.0.1:9123 Tested with PHP 7.1.10 on Windows 10 Solution To run PHP on Windows, you need to install Redistributable for Visual Studio, refer to the official PHP …

Read more

Nginx + PHP – No input file specified

A common Nginx + PHP error message “No input file specified.” nginx.conf http { include mime.types; default_type application/octet-stream; server { listen 80; server_name localhost; location / { root www; index index.html index.htm; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9999; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } Tested with Nginx 1.12.1 + PHP …

Read more

Mac OSX – Redirect .htaccess is not working ?

Here is the .htaccess file. /Library/WebServer/Documents/.htaccess # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /mkyong/ RewriteRule ^index\.php$ – [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /mkyong/index.php [L] </IfModule> # END WordPress Solution Find the Apache config file in /etc/apache2/httpd.conf, make sure the following settings are configured properly : 1. The mod_rewrite.so is loaded. …

Read more

Mac OSX – where is default localhost folder?

On Mac OSX, the default localhost folder is located at /Library/WebServer/Documents P.S Tested with MacOS Sierra, 10.12.6 Find httpd.conf By default, Apache is enabled and installed in /etc/apache2/, inside httpd.conf file, find DocumentRoot to tell where is the default localhost folder. sudo vim /etc/apache2/httpd.conf # DocumentRoot: The directory out of which you will serve your …

Read more

Gradle – Could not find method compileOnly

Git clone a new project, Gradle build and hits the following error message : Terminal $ gradle clean build FAILURE: Build failed with an exception. * Where: Build file ‘/Users/mkyong/Documents/workspace/hc2/web/build.gradle’ line: 25 * What went wrong: A problem occurred evaluating project ‘:web’. > Could not find method compileOnly() for arguments [org.springframework.boot:spring-boot-starter-tomcat] on project ‘:web’. * …

Read more

MongoDB – Where is the log file?

Default, MongoDB creates the log file at this path /var/log/mongodb/mongodb.log, if the log file is not found, please check with the MongoDB config file. logpath Check the MongoDB config file at /etc/mongod.conf or /yourMongoDBpath/mongod.conf, the logpath defined where to log. /etc/mongod.conf $ cat /etc/mongod.conf # mongod.conf # Where to store the data. # Note: if …

Read more

How to edit hosts file on Mac OSX

Edit this file /private/etc/hosts. Terminal $ sudo nano /private/etc/hosts ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost 127.0.0.1 mkyong-mac 198.x.x.x linode-bot1 1.2.3.4 www.google.com References What is the difference between …

Read more

MongoDB – bind() failed errno:99 Cannot assign requested address for socket

Review the server environment, we need to allow Server B to access the Server A MongoDB database. Server A – MongoDB server Private IP – 192.168.162.129 / 17 Server B – Application Server Private IP – 192.168.204.205 / 17 Update the bind_ip, but unable to start mongod process anymore. /etc/mongod.conf #$ vim /etc/mongod.conf # Listen …

Read more

MongoDB – Failed to unlink socket file /tmp/mongodb-27017

Upgraded MongoDB and unable to start the mongod process, review the /var/log/mongodb/mongod.log file, and found the following error messages : Terminal 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] MongoDB starting : pid=31927 port=27017 dbpath=/var/lib/mongodb3 64-bit host=hcompass 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] db version v3.2.16 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] git version: 056bf45128114e44c5358c7a8776fb582363e094 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t …

Read more

Java – How to print a Pyramid

A Java example to print half and full pyramid, for fun. CreatePyramid.java package com.mkyong; import java.util.Collections; public class CreatePyramid { public static void main(String[] args) { int rows = 5; System.out.println("\n1. Half Pyramid\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } …

Read more

PDFBox – How to read PDF file in Java

This article shows you how to use Apache PDFBox to read a PDF file in Java. 1. Get PDFBox pom.xml <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.6</version> </dependency> 2. Print PDF file Example to extract all text from a PDF file. ReadPdf.java package com.mkyong; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.PDFTextStripperByArea; import java.io.File; import java.io.IOException; public class ReadPdf { …

Read more

Python – How to print a Pyramid

A simple Python example to print half and full pyramid, just for fun. def half_pyramid(rows): print(‘Half pyramid…\n’) for i in range(rows): print(‘*’ * (i+1)) def full_pyramid(rows): print(‘\nFull pyramid…\n’) for i in range(rows): print(‘ ‘*(rows-i-1) + ‘*’*(2*i+1)) def inverted_pyramid(rows): print(‘\nInverted pyramid…\n’) for i in reversed(range(rows)): print(‘ ‘*(rows-i-1) + ‘*’*(2*i+1)) half_pyramid(5) full_pyramid(5) inverted_pyramid(5) Output Half pyramid… * …

Read more

Oracle PL/SQL – Check the Trigger status

Check the USER_TRIGGERS table, you can get the Trigger status easily : — display all triggers for users SELECT TRIGGER_NAME,STATUS FROM USER_TRIGGERS; — display status for a specified trigger SELECT TRIGGER_NAME,STATUS FROM USER_TRIGGERS WHERE TRIGGER_NAME = ‘TRIGGER_NAME’; SELECT TRIGGER_NAME,STATUS FROM USER_TRIGGERS WHERE TRIGGER_NAME IN(‘TRIGGER_NAME_A’, ‘TRIGGER_NAME_B’); Sample data. TRIGGER_NAME STATUS TRIGGER_NAME_A ENABLED TRIGGER_NAME_B DISABLED TRG_BEFORE_EMP_UPDATE ENABLED …

Read more

cURL – PUT request examples

Example to use cURL -X PUT to send a PUT (update) request to update the user’s name and email. Terminal $ curl -X PUT -d ‘name=mkyong&[email protected]’ http://localhost:8080/user/100 If the REST API only accepts json formatted data, try this Terminal $ curl -X PUT -H "Content-Type: application/json" -d ‘{"name":"mkyong","email":"[email protected]"}’ http://localhost:8080/user/100 References cURL official website cURL – …

Read more

cURL – DELETE request examples

To send a DELETE request, uses cURL -X DELETE Terminal $ curl -X DELETE http://localhost:8080/user/100 The above example will delete a user where user id is 100. References cURL official website cURL – POST request examples

How to tell Maven to use Java 8

In pom.xml, defined this maven.compiler.source properties to tell Maven to use Java 8 to compile the project. 1. Maven Properties Java 8 pom.xml <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> Java 7 pom.xml <properties> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.source>1.7</maven.compiler.source> </properties> 2. Compiler Plugin Alternative, configure the plugin directly. pom.xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> …

Read more

Spring Boot and Mustache – default value

In Spring Boot + Mustache template environment, if we didn’t assign a value to a {{variable}} on the Mustache’s page, the jmustache will throws the following error messages : com.samskivert.mustache.MustacheException$Context: No method or field with name ‘variable’ on line xx at com.samskivert.mustache.Template.checkForMissing(Template.java:316) ~[jmustache-1.13.jar:na] at com.samskivert.mustache.Template.getValue(Template.java:224) ~[jmustache-1.13.jar:na] at com.samskivert.mustache.Template.getValueOrDefault(Template.java:269) ~[jmustache-1.13.jar:na] P.S Tested with Spring Boot 1.5.2.RELEASE …

Read more

Spring Boot Hello World Example – Mustache

A Spring Boot web application example, using embedded Tomcat + Mustache template engine, and package as an executable JAR file. Technologies used : Spring Boot 1.5.2.RELEASE Spring 4.3.7.RELEASE jmustache 1.13 Thymeleaf 2.1.5.RELEASE Tomcat Embed 8.5.11 Maven 3 Java 8 Note Spring Boot uses jmustache to integrate Mustache as template engine. 1. Project Directory 2. Project …

Read more

Java – Convert comma-separated String to a List

Java examples to show you how to convert a comma-separated String into a List and vice versa. 1. Comma-separated String to List TestApp1.java package com.mkyong.utils; import java.util.Arrays; import java.util.List; public class TestApp1 { public static void main(String[] args) { String alpha = "A, B, C, D"; //Remove whitespace and split by comma List<String> result = …

Read more