我有这个类,它只发送一个 http post 请求:
import java.net.*; import java.io.*;
public class JarRuntimeTest{
public void start() throws Exception{
HttpURLConnection connection = (HttpURLConnection) (new URL("https://google.com").openConnection());
connection.setRequestMethod("POST");
System.out.println(
getResult((connection.getResponseCode() == HttpURLConnection.HTTP_OK)?
connection.getInputStream():connection.getErrorStream()));
}
public String getResult(InputStream in) throws Exception{
StringBuilder builder = new StringBuilder("");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) builder.append(line);
reader.close();
return builder.toString();
}
public static void main(String[] args){try{new JarRuntimeTest().start();}catch(Exception e){e.printStackTrace();}}
}
Run Code Online (Sandbox Code Playgroud)
我有这个.bat文件,它为其构建 jar 和 java 运行时映像,并使用此自定义运行时映像来运行我的 jar:
@echo off
javac …Run Code Online (Sandbox Code Playgroud)