Skip to main content

Java FAQ

javac 乱码

强制使用 UTF-8

javac -J-Dfile.encoding=UTF-8

环境变量

  • JAVA_TOOL_OPTIONS
    • java, javac, jar 会用
    • 例如 -agentlib:hprof
    • JVMTI 定义
  • _JAVA_OPTIONS
    • 类似 JAVA_TOOL_OPTIONS,但优先级更高
    • 针对 Hotspot
  • JAVA_OPTS
    • 由其他工具使用
    • java 不用
  • JDK_JAVA_OPTIONS
    • jdk9+
    • 类似 JAVA_TOOL_OPTIONS,但只有 java 会用
    • java
  • JAVA_HOME

Property

file.encoding=UTF-8
user.language=en
user.country=US
user.variant=
java.io.tmpdir=
javax.net.ssl.keyStore=
javax.net.ssl.keyStorePassword=
javax.net.ssl.keyStoreType=
javax.net.ssl.trustStore=
javax.net.ssl.trustStorePassword=
javax.net.ssl.trustStoreType=
com.sun.management.jmxremote=

选择 JUL 类逻辑

Why is generic of a return type erased when there is an unchecked conversion of a method parameter in Java 8?

Proxy performance

https://stackoverflow.com/q/1856242/1870054

deploy

  • -Dspring.profiles.active=development
  • SPRING_PROFILES_ACTIVE

A fatal error has been detected by the Java Runtime Environment

# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00000000000207f6, pid=1, tid=0x00007f9256df8b38
#
# JRE version: OpenJDK Runtime Environment (8.0_345-b01) (build 1.8.0_345-b01)
# Java VM: OpenJDK 64-Bit Server VM (25.345-b01 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 3.24.0
# Distribution: Custom build (Tue Nov 8 21:00:08 UTC 2022)
# Problematic frame:
# C 0x00000000000207f6
  • netty musl
    • -Dio.micrometer.shaded.io.netty.transport.noNative=true
    • -Dio.grpc.netty.shaded.io.netty.transport.noNative=true
    • -Dorg.apache.rocketmq.shaded.io.grpc.netty.shaded.io.netty.transport.noNative=true
    • Netty 4.1.77.Final+ 避免 segfaulting
apk add gcompat
export LD_PRELOAD=/lib/libgcompat.so.0

Unable to get pid of LinuxThreads manager thread

alpine docker 里 java 8 为 pid 1 时出现。

curl -O https://arthas.aliyun.com/arthas-boot.jar
pid=1
touch /proc/${pid}/cwd/.attach_pid${pid} && kill -SIGQUIT ${pid} && sleep 2 && ls /proc/${pid}/root/tmp/.java_pid${pid}
java -jar arthas-boot.jar 1
# musl 无法判断线程模型
getconf GNU_LIBPTHREAD_VERSION

# https://github.com/jattach/jattach
apk add jattach
jattach 1 properties

Java Proxy

  • properties
    • socksProxyHost
    • socksProxyPort=1080
    • socksNonProxyHosts
    • java.net.socks.username
    • java.net.socks.password
    • http.proxyHost
    • http.proxyPort
    • http.nonProxyHosts
      • e.g. localhost|host.mydomain.com
    • ftp.proxHost
    • ftp.proxyPort
    • ftp.nonProxyHosts
  • Datagrip 支持 ?socksProxyHost=&socksProxyPort=
JAVA_FLAGS=-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8087
java %JAVA_FLAGS%
// proxySet 似乎是无效的
if (needsProxy()) {
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", getProxyHost());
System.getProperties().put("proxyPort", getProxyPort());
} else {
System.getProperties().put("proxySet", "false");
System.getProperties().put("proxyHost", "");
System.getProperties().put("proxyPort", "");
}


gradle

systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=8080

Virtual Thread

Spring Boot Tomcat

  • 性能更好
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executors;

@SpringBootApplication
@Slf4j
public class VirtualthreadApplication {

public static void main(String[] args) {
SpringApplication.run(VirtualthreadApplication.class, args);
}

@Bean
public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
return protocolHandler -> {
log.info("Configuring " + protocolHandler + " to use VirtualThreadPerTaskExecutor");
protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
};
}
}