Tag Archives: java

Error reporting of uncapped XML character


today when writing the JSP to such a mistake, Unescaped XML character,
solution is as follows:

select Editor – Inspections, and find the HTML on the right side of Malformed content of the script tag, uncheck the, click OK, and successfully solve the problem of error!

Spring integrates JUnit configuration

2. In junit unit tests, executing
junit without a main method determines which methods in the current Test class have @test annotations
junit lets annotated methods execute
. Junit doesn’t matter if we’re using the spring framework
4. When the test method executes without an IOC container, a null pointer exception is raised even when the @Autowried annotation is written
Solution:
Spring integration junit configuration
1. 2. Replace the main method with the @runwith
3 provided by Spring with an annotation provided by junit. Runner told spring, spring and ioc creation is based on XML and annotation, and show the position of
annotation-based: @ ContextConfiguration (classes = SpringConfiguration. Class)
based on the configuration file: @ ContextConfiguration (locations = “classpath: bean. XML”)

When IntelliJ idea is programmed, 1. Black thick line or black cursor appears; 2. The solution of carriage return unable to wrap. (practical recommendation method 2)

Intellij idea programming 1. Black thick lines or black cursor 2. A solution that cannot wrap a line by carriage return. (Practical Recommendation Method 2)

reason: it is usually caused by accidentally pressing insert. Some computers abbreviate insert, putting Del and Ins together, and Ins is the insert key.
Method 1. Click Intellij Idea on the upper left corner of the main interface, File> Settings> The diagram below * * * *
Put him on the Use block caret, and you can get back to your old self. ***

Method 2 (recommended) : Press and hold the Fn+ INSERT key to restore directly.
note: do not press Fn+insert if Use block caret is checked, otherwise the enter key will fail to wrap.
PS: restore the enter enter key newline function solution:
cancel Use block caret in the check box (default is no check, no change can be ignored), and then hold down Fn+insert key, so that the cursor and return will return to the original.

Solution to the problem of Chinese garbled in gradle console in idea

1. Background:

Mowday, Xiaoming received the task from his superior and needed to develop a Gradle project in IDEA. When writing the test case, the following error occurred


2. The environment is as follows:
The IDEA 2019.3 Gradle 5.2.1 Java 1.8.0 comes with
3. Try the following
In the IDEA menu File==> Settings==> Edit==> Various UTF-8 are set in File Encoding. No effect. is also configured with UTF-8 in Gradle RunConfigurations. Still no effect of
Gradle and Gradle. Bat both join set DEFAULT_JVM_OPTS=” -dfile.Encoding =UTF-8″. Still not encoding!!
4. Final solution
Edit Help | Edit Custom VM Options in IDEA, adding the following parameters

-Dfile.encoding=UTF-8


The effect is as follows, problem solved

 
conclusion
In the emergence of Chinese disorderly code problem. The first time I thought of the coding format.
A compilation error occurred during gradle execution. Chinese encoding and IDEA encoding are inconsistent. Therefore, ensuring consistent encoding of the encoding file and IDEA is the key to solving the problem.
In CUSTOMER VM option in IDEA, the file encoding can be customized as UTF-8.

Conversion between list and string array

List is converted to a String array

 public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("1");
list.add("2");
System.out.println(list);
String[] strings =list.toArray(new String[list.size()]);
for (String s:strings
     ) {
    System.out.println(s);
}
}

String array converted to List

  public static void main(String[] args) {
    String[] a=new String[]{"1","2"};

        List list= Arrays.asList(a);
        System.out.println(list);
    }

Eclipse relies on spring boot configuration processor, and there is no prompt for writing properties and YML

Problem description:
In eclipse, the Spring-boot-Configuration-Processor dependency has been added to the SpringBoot project through the @configurationProperties (Prefix = “person”) annotation and the YML file for data binding but still does not raise the issue.

<!--Import the configuration file processor, and the configuration file will be prompted for binding.-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

Solution process:
Add the following dependencies

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
</properties>

Then update project
right click – “maven-” update project
Maven install
right click – “run as -” maven install

After Java application is deployed in Linux environment, Chinese is displayed as square solution

Reason: The server has no corresponding font
Solutions:
In Java environments above JRE 5, Java automatically loads the fonts under the $JAVA_HOME/ JRE /lib/ Fonts directory. Link or copy text or Microsoft yahei fonts to this directory to solve.
 
For example, if Java application is deployed in Docker container, font files need to be put into the corresponding directory when building the image, as follows
1. Create a new directory under Project Resources, TTF, and put the font file into the directory
2. Add configuration to the Maven plug-in Docker-Maven-Plugin in poM configuration
configuration.resources
< resource>
& lt; targetPath> /< /targetPath>
& lt; directory> ${project.build.outputDirectory}< /directory> < ! — Resource directory –& GT;
& lt; include> ttf/*< /include> < ! — The font file in the TTF directory that the program depends on –& GT;
< /resource> # font file

ADD the vera.ttf/* $JAVA_HOME/jre/lib/fonts/fallback/
3. DockerFile new are as follows
# font file
ADD the vera.ttf/* $JAVA_HOME/jre/lib/fonts/fallback
 
Fonts files can be downloaded from the web. The Windows environment Fonts file is located in the directory C:\Windows\ \Fonts\; you can also get the corresponding font files
 
Extension :(if the server has installed the corresponding font, the jre directory can not put the font file)
The Linux server installs fonts
01. Install font related software packages

yum install -y fontconfig mkfontscale
02, copy fonts to the specified directory
cp simsun.ttc /usr/share/fonts/
Mkfontscale // font extension
mkfontdir // new font directory
fc-cache-fv // refresh cache
03. Verify font installation
fc-list //” song style “Chinese font has been installed successfully
 

JNI calls the add function of list in Java to flash back

JNI calls the add function of List in Java to flit back
New changes to the problem

The problem
Flash back code is as follows:
in recent applications, the need to transfer data in C to Java a structure, in this structure, there are GNSSObservation object type of List, GNSSObservation for custom structures, therefore in the jni invocation List are required for data transmission of the add function to add data into the List, but the program, when the function is called multiple times, flash back, specific code is as follows:

env – & gt; CallObjectMethod(GnssObservationList,GnssObservationListAddFunctionID,GnssObservation); Instead of running every time, the program runs for several minutes in a row, and the function is called about 40 times per second. The reason for the backout is never found, and the data that needs to be passed to Java is passed as a double array. The reason for the code flash back has not been solved, I hope you can give some suggestions on the cause of flash back.
The new change

Resttemplate Chinese garbled problem – available

RestTemplate Chinese garbled code problem
Source code to see the cause of Chinese code solution

Causes the RestTemplate to receive parameters in the request response body with scrambled Chinese characters.
Source code to see the Reason for Chinese scrambled code


to take a closer look at the initialization parameter in the figure above, the default value of this parameter is as shown in the figure below.

you should know the cause of this problem and the solution. [after the RestTemplate is initialized, we will assign and modify it to the utf-8 code we need]
The solution
The SpringBoot project takes the following approach

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
  }

QuickFIX/J100% Java Open Source FIX Engine

http://www.quickfixj.org/
The Financial Information eXchange (FIX) protocol is a messaging standard developed specifically for the real-time electronic exchange of securities transactions. FIX is a public-domain specification owned and maintained by FIX Protocol, Ltd (FPL).
QuickFIX/J is a full featured messaging engine for the FIX protocol. It is a 100% Java open source implementation of the popular C++ QuickFIX engine.
Features:
Free! It costs nothing and has a very liberal open source licence. Full source code available (also at no cost). Supports FIX versions 4.0 – 4.4, 5.0/FIXT1.1. Runs on any hardware and operating system supported by 1.4+ Java SE or compatible VM. Compatibility with QuickFIX C++ Java Native Wrapper API (easy to upgrade)Java NIO asynchronous network communications for scalability (using Apache MINA)Supports embedded SSL with Java 5+Provides standard JMX MBeans for FIX engine management Easy to embed in existing Java applications.
Choice of message processing threading strategiesCommunication transports for TCP sockets and VM pipes. Metadata-driven parsing and validation. Metadata-driven code generation of type-safe FIX message-related classes. Metadata API for use at application level (for example, FIX messaging UI). Support for protocol customizations (new messages, fields, constraints). Session state storage plugins: JDBC, File, SleepyCat/JE, In memoryLogging plugins: JDBC, File, SFL4J (supports JDK1.4 logging, Log4J, Commons Logging), Console, Composite
Failover and High Availability. Scheduling of session connections. Many automated unit and acceptance tests. Example applications: Simple Swing order entry UI and a console-based order execution simulator. Commercial support available from multiple sources.
 
About QuickFIX/J…
The Financial Information eXchange (FIX) protocol is a messaging standard developed specifically for the real-time electronic exchange of securities transactions. FIX is a public-domain specification owned and maintained by FIX Protocol, Ltd (FPL). The FPL mission is to improve the global trading process by defining, managing, and promoting an open protocol for real-time, electronic communication between industry participants, while complementing industry standards.
QuickFIX/J is a full featured messaging engine for the FIX protocol. It is a 100% Java open source implementation of the popular C++ QuickFIX engine. For more information see the QuickFIX/J web site.
Features:
Free! It costs nothing and has a very liberal open source licence. Full source code available (also at no cost). Supports FIX versions 4.0 – 4.4. Runs on any hardware and operating system supported by 1.4+ Java SE or compatible VM. Compatibility with QuickFIX C++ Java Native Wrapper API (easy to upgrade) Java NIO asynchronous network communications for scalability (using Apache MINA) Easy to embed in existing Java applications.
Choice of message processing threading strategies Communication transports for TCP sockets and VM pipes. Metadata-driven parsing and validation. Metadata-driven code generation of type-safe FIX message-related classes. Metadata API for use at application level (for example, FIX messaging UI). Support for protocol customizations (new messages, fields, constraints). Session state storage plugins: JDBC, File, SleepyCat/JE, In memory Logging plugins: JDBC, File, SFL4J (supports JDK1.4 logging, Log4J, Commons Logging), Console Failover and High Availability. Scheduling of session connections. Many automated unit and acceptance tests. Integrated SSL communications Example applications: Simple Swing order entry UI and a console-based order execution simulator. Commercial support available from multiple sources.
Building and Testing QuickFIX/J
Dependencies Building QuickFIX/J Generating the database for JDBC based store and log Testing QuickFIX/J
Using QuickFIX/J
Creating Your Application Configuration Acceptor Failover Support Dynamic Acceptor Session Definition Receiving Messages Sending Messages Repeating Groups User Defined Fields Validation Secure communications using SSL Managing QFJ Applications With JMX Configuring character sets. Customizing Message Code Generation. Example Applications Determining your QFJ version.
 

Java – how to shuffle an ArrayList

In Java, you can use collections.shuffle to clean or rerun a ArrayList

TestApp.java

package com.mkyong.utils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestApp {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("A", "B", "C", "D", "1", "2", "3");

        //before shuffle
        System.out.println(list);

        // again, same insert order
        System.out.println(list);

        // shuffle or randomize
        Collections.shuffle(list);
        System.out.println(list);
        System.out.println(list);

        // shuffle again, different result
        Collections.shuffle(list);
        System.out.println(list);

    }

}

Output

[A, B, C, D, 1, 2, 3]
[A, B, C, D, 1, 2, 3]

[2, B, 3, C, 1, A, D]
[2, B, 3, C, 1, A, D]

[C, B, D, 2, 3, A, 1]

References

    Collections.shuffle JavaDoc