The JarRunner Class

简介:
The JarRunner application is launched with a command of this form:
java JarRunner url [arguments]
In the previous section, we've seen how   JarClassLoader   is able to identify and load the main class of a JAR-bundled application from a given URL. To complete the JarRunner application, therefore, we need to be able to take a URL and any arguments from the command line, and pass them to an instance of   JarClassLoader . These tasks belong to the   JarRunner   class, the entry point of the JarRunner application.

It begins by creating a java.net.URL object from the URL specified on the command line:

public static void main(String[] args) {
if (args.length < 1) {
usage();
}
URL url = null;
try {
url = new URL(args[0]);
} catch (MalformedURLException e) {
fatal("Invalid URL: " + args[0]);
}
...
If   args.length < 1 , that means no URL was specified on the command line, so a usage message is printed. If the first command-line argument is a good URL, a new   URL   object is created to represent it.

Next, JarRunner creates a new instance of JarClassLoader, passing to the constructor the URL that was specified on the command-line:

JarClassLoader cl = new JarClassLoader(url);
As we saw in the previous section, it's through   JarClassLoader   that JarRunner taps into the JAR-handling APIs.

The URL that's passed to the JarClassLoader constructor is the URL of the JAR-bundled application that you want to run. JarRunner next calls the class loader's getMainClassName method to identify the entry-point class for the application:

String name = null;
try {
name = cl.getMainClassName();
} catch (IOException e) {
System.err.println("I/O error while loading JAR file:");
e.printStackTrace();
System.exit(1);
}
if (name == null) {
fatal("Specified jar file does not contain a 'Main-Class'" +
" manifest attribute");
}
The key statement is highlighted in bold. The other statements are for error handling.

Once JarRunner has identified the application's entry-point class, only two steps remain: passing any arguments to the application and actually launching the application. JarRunner performs these steps with this code:

// Get arguments for the application
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
// Invoke application's main class
try {
cl.invokeClass(name, newArgs);
} catch (ClassNotFoundException e) {
fatal("Class not found: " + name);
} catch (NoSuchMethodException e) {
fatal("Class does not define a 'main' method: " + name);
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
System.exit(1);
}

Recall that the first command-line argument was the URL of the JAR-bundled application. Any arguments to be passed to that application are therefore in element 1 and beyond in the args array. JarRunner takes those elements, and creates a new array called newArgs to pass to the application (bold line above). JarRunner then passes the entry-point's class name and the new argument list to the invokeClassmethod of JarClassLoader. As we saw in the previous section, invokeClass will load the application's entry-point class, pass it any arguments, and launch the application.

本文转自博客园沉睡森林@漂在北京的博客,原文链接:The JarRunner Class,如需转载请自行联系原博主。

目录
相关文章
|
关系型数据库 MySQL Go
MySQL数据库安装(超详细完整步骤)
MySQL数据库安装(超详细完整步骤)
1982 1
|
SQL 大数据 关系型数据库
开源大数据比对平台(dataCompare)新版本发布
开源大数据比对平台(dataCompare)新版本发布
913 0
|
机器学习/深度学习 人工智能 自然语言处理
谷歌工程师Alex Irpan:2028年有10%概率实现AGI
【2月更文挑战第20天】谷歌工程师Alex Irpan:2028年有10%概率实现AGI
288 6
谷歌工程师Alex Irpan:2028年有10%概率实现AGI
|
机器学习/深度学习 算法 大数据
[ICLR 2024] 基于Pathways架构的自适应多尺度时间序列预测模型Pathformer
阿里云计算平台大数据基础工程技术团队主导,与华东师范大学数据科学与工程学院合作的论文《Pathformer: Multi-Scale Transformers With Adaptive Pathways For Time Series Forecasting》被ICLR 2024接收,该论文提出了基于Pathways架构的自适应多尺度时间序列预测模型Pathformer,它从时间分辨率和时间距离角度进行多尺度时序建模,同时进一步提出自适应Pathways来动态调整多尺度建模过程,基于两者,Pathformer在阿里云数据集和公开数据集上取得SOTA预测效果,并展现出不错的泛化性和迁移性。
|
消息中间件 JSON uml
6 张图告诉你 RocketMQ 是怎么保存偏移量的
6 张图告诉你 RocketMQ 是怎么保存偏移量的
481 1
6 张图告诉你 RocketMQ 是怎么保存偏移量的
|
机器学习/深度学习 人工智能 并行计算
|
1天前
|
数据采集 人工智能 安全
|
10天前
|
云安全 监控 安全