search
HomeJavajavaTutorialShell script implements method of running Java program jar

This article mainly introduces the method of running java program jar by shell script. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

When deploying projects on UBuntu, we often start the program through a shell, or even call the java program regularly through the crontab scheduled task, but there is a very strange problem That is, for example, I wrote the following shell script:


#!/bin/sh
export mypath=/root/project/wishnomal

java -Xmx3000m -Xms3000m -server -d64 -Dfile.encoding=UTF-8 -Dfetch.threads=300 -classpath $mypath/:$mypath/wish2-assembly-1.0.0.jar newstandard.CrawlerNewStandard $*

echo "END"

When I run the script manually from the command line, I can run the java program normally, but using crontab scheduled tasks, it seems that It won’t work

Analysis of possible reasons:

1) Whether the current user does not have executable permissions for this shell script, use ls -lrt /apps/service/mtk/checking/run. sh checks that the script is executable, but it has execution permission -rwxr-xr-x

2) Since there is no problem running the script alone, is it a timing problem? So I wrote a simple output shell script and it was no problem through timing. The problem is still with the script.

Later I checked online and thought it might be the environment variables in the script, because when running the script through crontab, the root user is used instead of the current user, so I cat /etc/profile to check the environment variables and then modify them. The script is as follows:

Analysis of possible reasons:

1) Whether the current user does not have executable permissions for this shell script, check through ls -lrt /apps/service/mtk/checking/run.sh The script is executable, but it has execution permission -rwxr-xr-x

2) Since there is no problem running the script alone, is it a timing issue? So I wrote a simple output shell script and it was no problem through timing. The problem is still with the script.

Later I checked online and thought it might be the environment variables in the script, because when running the script through crontab, the root user is used instead of the current user, so I cat /etc/profile to check the environment variables and then modify them. The script is as follows:


#!/bin/sh
export mypath=/root/project/wishnomal
export JAVA_HOME=/root/lib/jdk1.7.0_72
PATH=$PATH:$JAVA_HOME/bin

java -Xmx3000m -Xms3000m -server -d64 -Dfile.encoding=UTF-8 -Dfetch.threads=300 -classpath $mypath/:$mypath/wish2-assembly-1.0.0.jar newstandard.CrawlerNewStandard $*

echo "END"

export displays the environment variables exported as user environment variables

In this way, the crontab scheduled tasks will be normal.

Modification reference:


#!/bin/sh 
# ----------------------------------------------------------------------------- 
# Start script for the CMGP BOSSCONTROL  
# 
# $Id: run_bosscontrol.sh,v 1.0 2007/11/06 Exp $ 
# ----------------------------------------------------------------------------- 
#指定字符集 
LANG=zh_CN.GBK export LANG 
RUN_HOME=. 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/checking.jar 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/ojdbc14.jar 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/commons-dbutils-1.1.jar 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/log4j-1.2.14.jar 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/dom4j-1.6.jar 
 
export CLASSPATH 
 
java com.**.checking.Checking_Start >> log.out &

When you run the script manually from the command line, you can run the java program normally, but it seems impossible to use crontab scheduled tasks. It worked, but I was very depressed. I checked the reason and analyzed the possible reasons:

1) Whether the current user does not have executable permissions for this shell script, use ls -lrt /apps/service/mtk/checking/run .sh shows that the script is executable, but it has execution permission -rwxr-xr-x

2) Since there is no problem running the script alone, is it a timing problem? So I wrote a simple output shell script and it was no problem through timing. The problem is still with the script.

Later I checked online and thought it might be the environment variables in the script, because when running the script through crontab, the root user is used instead of the current user, so I cat /etc/profile to check the environment variables and then modify them. The script is as follows:


#!/bin/sh 
# ----------------------------------------------------------------------------- 
# Start script for the CMGP BOSSCONTROL  
# 
# $Id: run_bosscontrol.sh,v 1.0 2007/11/06 Exp $ 
# ----------------------------------------------------------------------------- 
export PATH=/apps/usr/java/jdk1.5/bin:$PATH 
export JAVA_HOME=/apps/usr/java/jdk1.5 
export JRE_HOME=/apps/usr/java/jdk1.5/jre 
export CLASSPATH=/apps/usr/java/jdk1.5/lib:/apps/usr/java/jdk1.5/jre/lib:$CLASSPATH 
RUN_HOME=/apps/service/checking 
CLASSPATH=$CLASSPATH$RUN_HOME/lib/checking.jar 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/ojdbc14.jar 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/commons-dbutils-1.1.jar 
CLASSPATH=$CLASSPATH:$RUN_HOME/lib/log4j-1.2.14.jar 
 CLASSPATH=$CLASSPATH:$RUN_HOME/lib/dom4j-1.6.jar 
 
export CLASSPATH=$CLASSPATH 
 
java com.**.checking.Checking_Start >> log.out &

export displays the environment variables exported as user environment variables

The above jar package is exported through the eclipse tool export. The MANIFEST.MF file is not included. If we use the packaging tool Ant, we can set Class-Path

in the default build.xml file and add the third-party jar package to the manifest.mf file, and Specify the main class of the program

Add the following content in build.xml:


<!-- create a property containing all .jar files, prefix lib/, and seperated with a space --> 
<pathconvert property="libs.project" pathsep=" "> 
  <mapper> 
   <chainedmapper> 
    <!-- remove absolute path --> 
    <flattenmapper /> 
    <!-- add lib/ prefix --> 
    <globmapper from="*" to="lib/*" /> 
   </chainedmapper> 
  </mapper> 
   <path> 
   <!-- lib.home contains all jar files, in several subdirectories --> 
   <fileset dir="${lib.dir}"> 
   <include name="**/*.jar" /> 
   </fileset> 
   </path> 
 </pathconvert>

In addition, when creating the manifest file, add:


<!-- 这样就可以将第三方jar包加入 -->  
<attribute name="Class-Path" value="${libs.project}" /> 
<!-- 程序运行的主类 --> 
<attribute name="Main-Class" value="com.**.checking.Checking_Start " />

Run ant like this, the content of MANIFEST.MF in the resulting jar package is as follows:


Manifest-Version: 1.0 
Ant-Version: Apache Ant 1.7.0 
Created-By: 1.5.0_09-b01 (Sun Microsystems Inc.) 
Implementation-Title: fee task 
Implementation-Version: 1.0 
Implementation-Vendor: Aspire 
Main-Class: com.aspire.cmgp.flowcontrol.server.FlowControlServer 
Class-Path: lib/cmgp-util-1.0.1.jar lib/commons-codec-1.3.jar lib/comm 
 ons-collections.jar lib/commons-dbcp-1.2.1.jar lib/commons-httpclient 
 .jar lib/commons-logging.jar lib/commons-pool-1.2.jar lib/dom4j.jar l 
 ib/log4j.jar lib/ojdbc14.jar

Like this There is no need to specify the jar package required by the program in the shell script, and there is no annoying problem of setting environment variables. This is how the more formal ones operate.

Just run the jar package directly in the shell: java -jar main program.jar -Xmx1024m -Xms1024m -Xmn512m, append # after

#!/bin/bash

##source /etc/profile

source ~/.bash_profile

Test. .


#! /bin/sh
export JAVA_HOME=/usr/java/jdk1.6.0_18
export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
for i in lib/*.jar; 
    do CLASSPATH=$i:${CLASSPATH} 
done
export CLASSPATH=.:${CLASSPATH}

java -cp ${CLASSPATH} The package name where the main method is located. The class name where the main method is located

The above is the detailed content of Shell script implements method of running Java program jar. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在 Windows 11 上安装经典 Shell?如何在 Windows 11 上安装经典 Shell?Apr 21, 2023 pm 09:13 PM

&lt;p&gt;定制您的操作系统是让您的日常生活更加愉快的绝佳方式。您可以更改用户界面、应用自定义主题、添加小部件等等。因此,我们今天将向您展示如何在Windows11上安装ClassicShell。&lt;/p&gt;&lt;p&gt;该程序已经存在了很长时间,并允许您修改操作系统。志愿者现在已经开始运营该组织,该组织于2017年解散。新项目名为OpenShell,目前在Github上可供感兴趣的人使用。&lt;/p&gt;&a

Explorer.exe 在系统启动时不启动 [修复]Explorer.exe 在系统启动时不启动 [修复]Jun 03, 2023 am 08:31 AM

如今,许多Windows用户开始遇到严重的Windows系统问题。问题是系统加载后Explorer.exe无法启动,用户无法打开文件或文件夹。虽然,Windows用户在某些情况下可以使用命令提示符手动打开Windows资源管理器,并且每次系统重新启动或系统启动后都必须这样做。这可能是有问题的,并且是由于下面提到的以下因素造成的。损坏的系统文件。启用快速启动设置。过时或有问题的显示驱动程序。对系统中的某些服务进行了更改。修改后的注册表文件。请记住以上所有因素,我们提出了一些肯定会对用户有所帮助

PowerShell 部署失败并出现 HRESULT 0x80073D02 问题修复PowerShell 部署失败并出现 HRESULT 0x80073D02 问题修复May 10, 2023 am 11:02 AM

您在运行脚本时是否看到此错误消息“Add-AppxPackage:部署失败,HRESULT:0x80073D02,无法安装该包,因为它修改的资源当前正在使用中。PowerShell中出现错误0x80073D02…”?如错误消息所述,当用户在前一个进程运行时尝试重新注册一个或所有WindowsShellExperienceHost应用程序时,确实会发生这种情况。我们已经获得了一些简单的解决方案来快速解决这个问题。修复1–终止体验主机进程您必须在执行powershell命令之前结束

Linux快速删除文件末尾行的操作步骤Linux快速删除文件末尾行的操作步骤Mar 01, 2024 pm 09:36 PM

Linux系统下在处理文件时,有时候需要删除文件末尾的行。这种操作在实际应用中很常见,可以通过一些简单的命令来实现。本文将介绍在Linux系统中快速删除文件末尾行的操作步骤,同时提供具体的代码示例。步骤一:查看文件末尾行在进行删除操作之前,首先需要确认文件的末尾行是哪一行。可以使用tail命令来查看文件的末尾行,具体命令如下:tail-n1filena

在 Windows 上运行 shell 脚本文件的不同方法在 Windows 上运行 shell 脚本文件的不同方法Apr 13, 2023 am 11:58 AM

适用于 Linux 的 Windows 子系统第一种选择是使用适用于 Linux 或 WSL 的 Windows 子系统,这是一个兼容层,用于在 Windows 系统上本地运行 Linux 二进制可执行文件。它适用于大多数场景,允许您在 Windows 11/10 中运行 shell 脚本。WSL 不会自动可用,因此您必须通过 Windows 设备的开发人员设置启用它。您可以通过转到设置 > 更新和安全 > 对于开发人员来完成。切换到开发人员模式并通过选择是确认提示。接下来,查找 W

超硬核!11个非常实用的 Python 和 Shell 拿来就用脚本实例!超硬核!11个非常实用的 Python 和 Shell 拿来就用脚本实例!Apr 12, 2023 pm 01:52 PM

Python 脚本部分实例:企业微信告警、FTP 客户端、SSH 客户端、Saltstack 客户端、vCenter 客户端、获取域名 ssl 证书过期时间、发送今天的天气预报以及未来的天气趋势图;Shell 脚本部分实例:SVN 完整备份、Zabbix 监控用户密码过期、构建本地 YUM 以及上篇文章中有读者的需求(负载高时,查出占用比较高的进程脚本并存储或推送通知);篇幅有些长,还请大家耐心翻到文末,毕竟有彩蛋。Python 脚本部分企业微信告警此脚本通过企业微信应用,进行微信告警,可用于

以下是 Open Shell Windows 11 无法正常工作问题的修复以下是 Open Shell Windows 11 无法正常工作问题的修复Apr 14, 2023 pm 02:07 PM

无法在Windows 11上运行的 Open shell 并不是一个新问题,并且自从这个新操作系统问世以来一直困扰着用户。Open-Shell Windows 11 不工作问题的原因并不具体。它可能是由程序中的意外错误、病毒或恶意软件的存在或损坏的系统文件引起的。对于那些不知道的人,Open-Shell 是 2017 年停产的 Classic Shell 的替代品。您可以查看我们的教程,了解如何在 Windows 11 上安装 Classic Shell。如何替换 Windows 11 的开始菜

如何安装 Open Shell 以恢复 Windows 11 上的经典开始菜单如何安装 Open Shell 以恢复 Windows 11 上的经典开始菜单Apr 18, 2023 pm 10:10 PM

OpenShell是一个免费的软件实用程序,可用于自定义Windows11开始菜单,使其类似于经典风格的菜单或Windows7样式的菜单。以前版本的Windows上的开始菜单为用户提供了一种浏览其系统内容的简单方法。基本上,OpenShell是ClassicShell的替代品,它提供了不同的用户界面元素,有助于从以前的Windows版本获取后一个版本的功能。一旦ClassicShell的开发在2017年停止,它就由GitHub志愿者以OpenShell的名义维护和开发。它与Win

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),