System call is a mechanism to execute command line commands from an application.
When can we make System calls?
From a Java application we can make system calls in such situations as to copy a file/shortcut from one location to another, delete a file, to hide a folder to save it from accidental delete, to run a shortcut file or launch another application from java during run time etc.
Below is an example which shows how to make system calls from Java
package com;
publicclass SystemCallExample {
/**
*Method tocopyshoutcut form one location to another
*
*/
privatevoid copyShoutcut()
{
String souceLocation="";
String destLocation="";
String fileCopyCmd="";
Process shortcutCpyPrs=null;
try{
souceLocation = "C:\\Source\\TextPad.lnk";
destLocation = "C:\\Destination";
fileCopyCmd = "copy \""+souceLocation +"\" \""+ destLocation+"\"";
shortcutCpyPrs = Runtime.getRuntime().exec(new String[]{"cmd","/c",fileCopyCmd});
}
catch(Exception ex)
{
if(shortcutCpyPrs!=null)
{
shortcutCpyPrs.destroy();
}
ex.printStackTrace();
System.exit(-1);
}
}
/**
*Method to execute shoutcut file
*
*/
privatevoid execShoutcut()
{
String shortcutPath="";
Process runShortcutPrs=null;
try{
shortcutPath = "C:\\Destination\\TextPad.lnk";
String runShortcutCmd="start "+ shortcutPath;
runShortcutPrs = Runtime.getRuntime().exec(new String[]{"cmd", "/c",runShortcutCmd});
runShortcutPrs.waitFor();
}
catch(Exception ex)
{
if(runShortcutPrs!=null)
{
runShortcutPrs.destroy();
}
ex.printStackTrace();
System.exit(-1);
}
}
/**
*Method to execute another application forex. AcrobatReader from Java
*
*/
privatevoid execAdobeAppl()
{
String shortcutPath="";
Process execAcrobatPrs=null;
try{
execAcrobatPrs = Runtime.getRuntime().exec(new String[]{"cmd", "/c","start AcroRd32.exe"});
//execAcrobatPrs.waitFor();
}
catch(Exception ex)
{
if(execAcrobatPrs!=null)
{
execAcrobatPrs.destroy();
}
ex.printStackTrace();
System.exit(-1);
}
}
publicstaticvoid main(String arg[])
{
try{
SystemCallExample sysCall=new SystemCallExample();
sysCall.copyShoutcut();
sysCall.execShoutcut();
sysCall.execAdobeAppl();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
System calls are platform dependent. So the above code will work only in Windows.