Posts

Showing posts with the label Java

Digital Marketing

Java reflection example

Reflection is the ability of a computer program to examine and modify the structure and behavior (specifically the values, meta-data, properties and functions) of an object at runtime. Type Introspection (instanceof) is the ability to inspect the code in the system and see object types. Reflection is then the ability to make modifications at runtime by making use of introspection. The sample code can be download here . output: Hello  goyun.info ! Hello  goyun.info ! Hello it.goyun.info! Use reflection to deal with private property or method such as unit testing: // / For private methods: Method method = targetClass . getDeclaredMethod(methodName, argClasses); method . setAccessible( true ); return method . invoke(targetObject, argObjects); // / And for private fields: Field field = targetClass . getDeclaredField(fieldName); field . setAccessible( true ); field . set(object, value); // The setAccessible(true) is required to play around with privates. Refl...

Java Http Client

Although the java.net package provides basic functionality for accessing resources via HTTP, it doesn't provide the full flexibility or functionality needed by many applications. HttpClient seeks to fill this void by providing an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations. Designed for extension while providing robust support for the base HTTP protocol, HttpClient may be of interest to anyone building HTTP-aware client applications such as web browsers, web service clients, or systems that leverage or extend the HTTP protocol for distributed communication. (Apache Commons HttpClient is an old project which  is now deprecated  that produced HttpClient 3.1. The project was subsumed by the larger Apache HttpComponents project, which produced HttpClient 4.x.) Restful implementation Jersey provides  ApacheHttpClient Class which is a Client that utilizes the Apache HTTP client to send and...

How to Install Oracle Java JDK in Ubuntu Linux

Copy and decompress oracle Java to /usr/local/java $ sudo mv jdk1.8.0_05/ /usr/local/java Edit profile: $ sudo gedit /etc/profile & Scroll down to the end of the file using your arrow keys and add the following lines below to the end of your /etc/profile file: JAVA_HOME=/usr/local/java/ PATH=$PATH:$HOME/bin:$JAVA_HOME/bin export JAVA_HOME export PATH Inform your Ubuntu Linux system where your Oracle Java JDK/JRE is located. This will tell the system that the new Oracle Java version is available for use. $ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/bin/java" 1 $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/bin/javac" 1 $ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/bin/javaws" 1 Inform your Ubuntu Linux system that Oracle Java JDK/JRE must be the default Java. $ sudo update-alternatives --set java ...

How to solve Java: not found problem with Glassfish

If you get an error when starting Glassfish similar to this: [supperman@ goyun.info ]# service glassfish3 start Starting Glassfish Service java glassfish/bin/asadmin: line 54: exec: java: not found You need to re-configure your Java path in your Glassfissh configuration file: Open glassfish/config/ asenv.conf  in your editor of choice and add path similar to the following line: AS_JAVA ="/usr/java/jdk1.8.0" Save the file and Glassfish service should start without problems. You can find your Java path by: which java

How to link to an external URL in Javadoc

Example: This creates a "See Also" heading containing the link, i.e.: /** * @see <a href="https:// goyun.info ">https:// goyun.info </a> */ Javadoc

Java Calendar iCal4j example

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter-propose another meeting date/time. iCalendar is used and supported by a large number of products, including Google Calendar, Apple Calendar (formerly iCal), IBM Lotus Notes, Yahoo! Calendar, Evolution (software), eM Client, Lightning extension for Mozilla Thunderbird and SeaMonkey, and partially by Microsoft Outlook and Novell GroupWise. iCalendar is designed to be independent of the transport protocol. For example, certain events can be sent by traditional email or whole calendar files can be shared and edited by using a WebDav server, or SyncML. Simple web servers (using just the HTTP protocol) are often used to distribute iCalendar data about an event ...

Java Calendar iCal4j example

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter-propose another meeting date/time. iCalendar is used and supported by a large number of products, including Google Calendar, Apple Calendar (formerly iCal), IBM Lotus Notes, Yahoo! Calendar, Evolution (software), eM Client, Lightning extension for Mozilla Thunderbird and SeaMonkey, and partially by Microsoft Outlook and Novell GroupWise. iCalendar is designed to be independent of the transport protocol. For example, certain events can be sent by traditional email or whole calendar files can be shared and edited by using a WebDav server, or SyncML. Simple web servers (using just the HTTP protocol) are often used to distribute iCalendar data about an event ...

Using SLF4J / Logback in your Maven Project

Using slf4j: The only single mandatory dependency is slf4j-api.jar. If no binding is found on the class path, then SLF4J will default to a no-operation implementation. Sample usage: package info.goyun.maven; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App { final Logger logger= LoggerFactory.getLogger(App.class); public static void main ( String[] args ) { new App().sample(); } public void sample () { String f= " goyun.info " ; char [] s= new char [] { 'i' , '8' , '8' , '.' , 'c' , 'a' }; logger.debug( "First one is {}. Second one is {}." , f, s); } } Add one and only one dependency of the binding of your choice to your pom.xml. From the client's perspective all versions of slf4j-api are compatible. Client code compiled with slf4j-api-N.jar will run perfectly fine with slf4j-api-M.jar for any N and M. You only n...

Example of JAX-RS Client API in Java EE 7

try { Client client = ClientBuilder.newClient(); WebTarget target = client.target( "http:// goyun.info /webapi/user" ); String response = target.request(). get ( String . class ); } catch (IllegalArgumentException | NullPointerException | WebApplicationException ex) { logger.severe( "processing of HTTP response failed" ); }

Setting Windows File Associations to Open .JNLP Files Properly

Close any Collaborate session or recording windows. Open the Windows Control Panel and go into Programs and Features. Check the list of programs to ensure that Java is already installed on the system. If it isn't installed, please download and install Java from www.java.com before proceeding. Download a new Collaborate meeting or recording file but don't launch the file yet. Locate the meeting or recording .jnlp file in your Downloads folder. Right-click on the file and choose Open with then click Choose another app. If you see Java (TM) Web Start Launcher in this list (you may need to click More apps to expand the list), select it and check the box that says Always use this app to open .jnlp files then click the OK button. If Java (TM) Web Start Launcher is not in the list, check the box that says Always use this app to open .collab files then click Look for another app on this PC. Navigate to the following location: c:\program files\java\jreXXX\bin XXX represents a number tha...

Java Currency Formatter

Spring Boot supports the following embedded servlet containers

Tomcat 9.0 Jetty 9.4 Undertow 2.0 You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.

@SpringBootApplication annotation includes Auto- Configuration, Component Scan, and Spring Boot Configuration.

If you added @SpringBootApplication annotation to the class, you do not need to add the @EnableAutoConfiguration, @ComponentScan and @SpringBootConfiguration annotation. The @SpringBootApplication annotation includes all other annotations. Example:

The entry point of the spring boot application is the class contains @SpringBootApplication annotation and the main method.

Spring Boot automatically scans all the components included in the project by using @ComponentScan annotation. Spring Boot automatically configures your application based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation. For example, if MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.

How to import/open Spring Boot sample in IDE

The spring boot demos do not include specific IDE settings, as they are independent of the IDE. Spring boot uses maven as build tool, so you can import the project as existing maven project.

Find missing elements in a Integer array containing numbers.java

The java.util.BitSet.nextClearBit(int fromIndex) method returns the index of the first bit that is set to false that occurs on or after the specified starting index.

find missing number in array of size n containing numbers from 1 to n only.java

Java program illustrating Bitset Class

How To activate the EntityManagerFactory on Glassfish server

To activate the EntityManagerFactory, the application needs to be disabled and enabled when database connection is stable: ./asadmin disable myapp ./asadmin enable myapp