Using Java to invoke executable commands

Source: Internet
Author: User
Execute Package test;


/** * Add one Sentence class summary here. * Add class description here. * * @author lxx * @versi On 1.0, 2004-11-16 */public class testCMD {    public testCMD () {}  /* public  void MA In (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 "); //Call ping command        }catch ( exception  e)         {             e.printstacktrace ();           }                }&NBsp;  }  *///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.se       parator+ 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" >
<body><div align= "center" >
<%     runtime  runtime  =  runtime.getruntime ();     process  process  =null;     string  line=null;     inputstream  is  =null;& nbsp    inputstreamreader  isr=null;     bufferedreader  br  =null;      string  ip= "www.sina.com.cn"; //Pending ping address     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 )   &NBSP;&Nbsp;     {             out.println ( Line);             Out.flush ();         }         out.println ("</pre>");          is.close ();         isr.close ();          br.close ();    }     catch (ioexception  e )   & nbsp;  {         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.