Use Java to call executable command instances

Source: Internet
Author: User

Package test;

/**
* Add one sentence class summary here.
* Add class description here.
*
* @ Author lxx
* @ Version 1.0
*/
Public class TestCmd {
Public TestCmd (){}
/* Public void main (String args []) {
Try {
Process process = runtime.getruntime(cmd.exe c ("cmd.exe/c start http://www.csdn.net"); // log on to the website
Process process = runtime.getruntime(cmd.exe c ("cmd.exe/c start ping 10.144.98.100"); // call the Ping command
} Catch (Exception e)
{
E. printstacktrace ();
}

}
}*/

// Create a folder named Hello under the project
Public static void main (string [] ARGs ){
System. Out. println (system. getproperty ("user. dir "));
Createfolder ("hello ");
}

Private Static void createfolder (string Foldername ){
String temp = system. getproperty ("user. dir") + Java. Io. file. Separator + Foldername;
Java. Io. File F = new java. Io. File (temp );
F. mkdirs ();
}

}

InJavaObtain the path of the current running program in the program

Import java. Io .*;

Public class test {

Public static void main (string [] ARGs ){

File directory = new file (".");

Try {

File newpath = new file (directory. GetCanonicalPath () + "newfolder ");

Newpath. mkdir ();

} Catch (Exception exp)

{

Exp. printStackTrace ();

}

}

}

// File directory = new File (".");

// Directory. getCanonicalPath (); get the current path

Call the Ping command --- PingIP. Jsp on the jsp page
<% @ Page language = "java" contentType = "text/html; charset = gb2312" import = "java. io. *" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> Ping IP test page </title>
</Head>

<Body>
<Div align = "center">
<H2> Ping IP test page </Div>

<%
Runtime runtime = Runtime. getRuntime ();
Process process = null;
String line = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
String ip = "www.sina.com.cn"; // address to be pinged
Try
{
Process worker runtime.exe c ("ping" + ip );
Is = process. getInputStream ();
Isr = new InputStreamReader (is );
Br = new BufferedReader (isr );
Out. println ("<pre> ");
While (line = br. readLine ())! = Null)
{
Out. println (line );
Out. flush ();
}
Out. println ("</pre> ");
Is. close ();
Isr. close ();
Br. close ();
}
Catch (IOException e)
{
Out. println (e );
Runtime. exit (1 );
}
%>

</Body>
</Html>

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.