Your Site Title

Java exec

macOS JAVA env

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin 
/Library/Java/JavaVirtualMachines

sudo cp -r /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk
sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk

Linux 应用输入输出

Linux分为:

丢掉输出: ls 1>/dev/null 2>/dev/null

重定向输出: ls 1>/dev/null 2>&1

Java执行外部命令

ProcessBuild RunTime.getRunTime.exe

重定向外部命令:

builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);

// or use a convenient call for all at once (thanks VGR to mention it)
builder.inheritIO();

Java 参数带双引号和空格 需要特别处理

python datax.py -p”-Did=0 -Dname=abc” job.json

错误:

List<String> command = new ArrayList<>();
command.add("python");
command.add("datax.py");
command.add("-p\"-Did=0 -Dname=abc\""); // 错误
command.add("job.json");

正确:

List<String> command = new ArrayList<>();
command.add("python");
command.add("datax.py");
command.add("-p");
command.add("-Did=0 -Dname=abc"); // 错误
command.add("job.json");

ProcessBuild会自己处理空格 和 双引号

编译与运行

javac 使用-cp|-classpath 指定依赖 使用-g添加调试信息 java 使用-classpath 指定依赖 java main方法如果带有包名, 需要在项目根目录运行带包名的类

java -classpath ".;./lib/*" xxx.xxx.xxx.MainClass

Debug

JDB: The Java Debugger JPDA: Java platform debugger architecture

jdb -classpath .:./bin HelloJDB

# jar
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar

# tomcat
./catalina.sh jpda start 默认8000    
netstat -an | grep 8000
JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=1043,server=y,suspend=n" 修改端口

jdb -connect com.sun.jdi.SocketAttach:hostname=127.0.0.1,port=5005

Reference