Java projects under Windows hit the jar to write a script that runs under Windows and Linux (I'm ready to test!). )

Source: Internet
Author: User

Objective:

Recently the company has made a tool to package this tool into a running program, and writing Start.bat and start.sh can run under both Windows and Linux.

On the internet to find a lot of information, finally found a reliable information, recorded, share to everyone, also considered a summary of their own.

The draught does not forget to dig the well man, thanks this elder brother article: 78560715

Not many BB go straight to work!

I. Structure of the project

Because it's just a test, it's not complicated, it's easy to explain, it's a Maven project, and of course a common Java project is fine.

Second, in the local run through the confirmation of the error after the jar into the local, the specific steps are:

2.1 Create a folder named Zztest on the desktop.

Right click on the project-->export-->java-->jar file and click Next, such as:

2.2 . Settings. Classpath. Project these three files do not tick, do not tick!

Select the Zztest directory created by the path of the build jar package to 2.1 and click Next, such as:

2.3 Do not move anything, continue to click Next, such as:

2.4 Click Browse ... After setting the main class to run, click OK and click Finish, such as:

If you have a warning, don't worry, just make sure!

Third, view jar

A jar package named Test01.jar already exists in zztest, such as:

Four, decompression ( if the project has external jar references need to add the configuration here, if not please ignore this step to enter the sixth step!!! )

Right-click to unzip the Test01.jar directly to the current folder, such as:

V. Configure the MANIFEST.MF file ( If there is a reference to the external jar in the project need to add the configuration here, if not please directly ignore this step into the sixth step!!! )

5.1

Open a. manifest.mf file under Meta-inf

There's a space behind Class-path. Note that the space is followed by the path of the jar package. where the Lib directory is similar to the SRC directory

Such as:

If there is a case where there are no references to multiple jars in this article, please try to refer to more than one jar:

class-path:lib/First Jar

class-path:lib/nth Jar

5.2 Replace the modified MANIFEST.MF with the Meta-inf under the jar package MANIFEST.MF

Vi. Writing Start.bat Scripts

Create a text file in the Zztest directory, add a piece of code inside it, save it after the name is Start.bat, save it, make sure to save it, such as:

The final file structure ( If no external jar is referenced in the project, no fourth step, fifth step, only one Test01.jar and Start.bat):

Double-click on the Start.bat to run, as the result. This is the end of Windows!

Vii. start.sh startup scripting under Linux

This article uses SECURECRT and SECUREFX to operate the Linux terminal

7.1 Operation Securesecurefx the jar into the specified folder such as:

7.2 Input Unzip Test01.jar the jar decompression ( must be decompressed, to be able to run, I do not know the principle of decompression...), such as:

7.3 Create a start.sh script in the SECURECRT command line and specify the Java class where the main method is executed

VI start.sh NOTE: Create a script named start.sh with the following results:

Press "I" to enter edit mode to edit the file as shown:

#!bin/bash

java-xms50m-xmx250m Com.zhanzhuang.windows_and_linux. WAL

Exit

Then press ESC.

Then hold down SHIFT and add:

Can let go, enter WQ Click Enter

------------------------------------------------------------------------------

The cursor becomes as shown

7.4 sh start.sh execution script results as shown

At this point, the steps under Linux are also explained. Thank you for your small friends to watch!

If there are referenced external jar packages in the project running under Linux, the start.sh script contents are as follows

Java projects under Windows hit the jar to write a script that runs under Windows and Linux (I'm ready to test!). )

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.