Windows automation for Java-run Java programs automatically

Source: Internet
Author: User

So in the case of some tools complete and already have a certain written Java program (environment variables and software see the previous chapter http://www.cnblogs.com/xuezhezlr/p/7718273.html), how to automate the running of Java programs?

This article all paths are local absolute path, the reader should improvise, the path on their own machine and my path will differ, must change

First build a new Java program Helloword,, open the ideal, create a MAVEN project, import our JDK (the path please look for, is the Java installation location selected JDK folder). and create a Java project, the name is random, the author writes is 123,,,

On the other hand, we create new folders and Java programs under the newly created project, and then right-click the new package (this is the Java folder, Src->main->java). In theory, you should follow certain rules to place programs with similar usage or call relationships in a folder, and then right-click New Java Program

Write a sentence inside.

Public  static void Main (String args[]) {
System.out.printf ("Helloword");
}

The Helloword can be printed directly after running, which is how Java is run manually

Build a program, just the output becomes 123456

The following should be thought, so-called automation should be non-manual machine execution of a timing mechanism, absolutely not manual,,, so below how to let the machine to execute the above Java code, I will total 3 methods, compared to recommend the 3rd kind

1 starting with a compressed jar package

Open Xshell, enter the command CD C:\Users\007\IdeaProjects go to the engineering level of the project you just created, and then execute JAR-CVF zlr.jar 123, this command can pack 123 of this Java project into a Zlr.jar jar package

Then enter the C:\Users\007\IdeaProjects directory, you can see the Zlr.jar package exists in this directory, in this directory to create a new Zlr.bat file, which is written in these words

@echo off
Java-jar Zlr.jar
Pause

In fact, the. bat file is actually, we run the way, and here the @echo off and pause is to let the page pause, no words running results can not see,,, the middle of the sentence Java-jar Zlr.jar is the statement to run the jar file

Save after double-click Run, error

Here error does not have the main manifest file, modify method is, use RAR form open after Enter Meta-inf folder, look at MANIFEST.MF, inside now only two words

manifest-version:1.0
created-by:1.8.0_131 (Oracle Corporation)

Add the following sentence Main-class:zlr.shiyan, note the space after the colon, replace, run an error, cannot find or load the main class Zlr.shiyan

This method is really disgusting, we have to do is, the code in the first line of the package ZLR; remove, only hit the jar bag is no problem to run,,,

2 run with a class similar to the above

Based on the original code into the directory C:\USERS\007\IDEAPROJECTS\123\SRC\MAIN\JAVA\ZLR can see two Java files, this is our two poor program

Run the following command in Xshell

CD C:\USERS\007\IDEAPROJECTS\123\SRC\MAIN\JAVA\ZLR//Enter

Javac Shiyan.java//Package Shiyan.java This Java program, the generated class file is in the current directory

Javac Shiyan//Run Shiyan This just-generated class file

3 using Ant for triggering

In the two methods mentioned above, we realize that starting a Java program needs to be compiled in order to do other operations, and relatively troublesome, mainly if the Java project is relatively large, we have to do to delete the package declaration packages ZLR; This stuff, it's very unfriendly.

There are a few benefits to using ant to trigger the case.

1ant integration is good, you can import the jar package in the program to write out, you can also send mail and other operations

2ant using a class file, it is relatively simple to get

3ant on the package name, comments and other Java phenomena can be better compatible, Ant will also play logs ~

4ant Support * This operation, there are some tags, testng,jmeter and juint are strong support, and the above is basically only support main function (I really only know that they support the main function, the other temporarily do not know ~)

In general I personally like to use ant to trigger, now look at how to trigger

Under the path of that class, create an XML file that contains these

<?xml version= "1.0" encoding= "UTF-8"?>
<project name= "HelloWorld" default= "Run" basedir= "." >
<!--running the command is actually the Java command. Indicates the name of the class to run, and the path that can be well-known to run class-->
<target name= "Run" >
<java classname= "Shiyan" classpath= "C:\USERS\007\IDEAPROJECTS\123\SRC\MAIN\JAVA\ZLR"/>
</target>

</project>

In fact, this classpath and classname is not the same for everyone, to change, and then build a. bat file, write
Ant-f c:\users\007\ideaprojects\123\src\main\java\zlr\build18.xml>c:\users\007\ideaprojects\123\src\main\ Java\zlr\build18.log

The result will be written to Build18.log, showing that the operation was successful.

--------I'm a gorgeous split-line------

Here's how to run Java code with program ant, let's talk about how to use Windows ' timed tasks

The author is windows10, other operating systems please do their own exploration

Search for tasks, select Task Scheduler, right-click to create a basic task, such as

Click Next, select Launch Program, find our BAT file,,

This makes it possible to complete one of the simplest Windows timed triggers.

Trigger words In addition to this timing task, you can also use the Jenkis trigger, I have done, but that is another project, in general, if there is no deployment of things, the timing of triggering the use of this is OK

Exhausted me, so finally introduced the implementation of Windows Automation, in their own written Java code based on the use of timed tasks +.bat+ant+build->java can ~ have a chance to try it, the computer can do it

Windows automation for Java-run Java programs automatically

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.