Using Java to invoke an executable command instance

Source: Internet
Author: User
Execute Package test;


/**
* ADD one sentence class summary here.
* Add class description here.
*
* @author LXX
* @version 1.0, 2004-11-16
*/
public class testCMD {
Public testCMD () {}
/* public void Main (String args[]) {
try {
Process process = Runtime.getruntime (). EXEC ("cmd.exe/c start Http://www.csdn.net"); Login site
Process process = Runtime.getruntime (). EXEC ("cmd.exe/c start ping 10.144.98.100"); Invoke 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 ();
}

}



To get the path of the currently running program in the Java 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 in the JSP page---pingip.jsp
<%@ 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" >


<title>ping IP test Page </title>


<body>
<div align= "center" >
</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 Ping
Try
{
Process =runtime.exec ("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>






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.