Java程序打包后带参数执行
执行样式
java -jar xxx.jar 参数1 参数2
所需配置
maven
maven的pom文件夹中配置build相关内容,格式如下
<!--打包必须,生成jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--此处填写运行jar包时启动的main函数-->
<mainClass>com.xxxx.run.RunZB</mainClass>
<layout>JAR</layout>
<!--构建完整可执行程序,可以直接运行-->
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
上方用到的代码
public class RunZB {
public final static Logger logger = LoggerFactory.getLogger(RunZB.class);
private static String driver;
private static String url;
private static String username;
private static String password;
private static Connection conn=null;
private static Statement stat=null;
/**
* args[0]:sql文件路径
* args[1]:配置文件路径
* @param args
*/
public static void main(String[] args) {
String path1 = args[0];
String path2 = args[1];
test_autoCommit( FileUtils.file2str(path1), path2);
}
/**
* 原生jdbc,通过原生的事务来实现。
* @param TxtSql
*/
public static void test_autoCommit(List<String> TxtSql, String path) {
ByteArrayOutputStream baoS = new ByteArrayOutputStream();
String errorSql =null;
try {
connectOracle(path);
for (String sql : TxtSql) {
// logger.info("[" + path + "]中包含" + TxtSql.size() + "条sql语句");
//4、执行sql语句
stat.execute(sql);
errorSql = sql;
// logger.info("sql语句:[" + sql + "]执行成功!");
}
conn.commit();
logger.info("文本成功执行!");
// logger.info("文本[" + path + "]成功执行!");
//5、处理结果集
} catch (Exception e) {
e.printStackTrace();
//异常信息输出到日志
e.printStackTrace(new PrintStream(baoS));
String s = baoS.toString();
logger.error(s);
// logger.error("文本[" + path + "]因异常终止!所有sql都不生效!,出错在 “" + errorSql + "”附近");
try {
conn.rollback();//如果sql有错误进行回滚
} catch (SQLException e1) {
e1.printStackTrace();
}
}
finally{
//6、关闭资源
try {
if(stat!=null)stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 连接Oracle数据库
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void connectOracle(String path) throws ClassNotFoundException, SQLException {
getConfig(path);
//1、注册驱动
Class.forName(driver);
//2、获取连接
conn= DriverManager.getConnection(url, username, password);
//关闭自动事务
conn.setAutoCommit(false);
//System.out.println(conn);
//3、创建statement对象
stat=conn.createStatement();
}
public static void getConfig(String path) {
List<String> filestr = FileUtils.filestr(path);
List<String> config= new ArrayList<>();
Map<String, String> map = new HashMap<>();
for (String s : filestr) {
config.add(s.substring(s.indexOf('=') + 1));
}
map.put("driver", config.get(0));
map.put("url", config.get(1));
map.put("username", config.get(2));
map.put("password", config.get(3));
url = map.get("url");
driver = map.get("driver");
username = map.get("username");
password = map.get("password");
}
执行的一个案例
不传参
不传参就会报如下错误