public class ShutdownMachine {
public static void main(String[] args)throws Exception {
String shutdownCommand;
String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem)
|| "Mac OS X".equals(operatingSystem)) {
shutdownCommand = "shutdown -h now";
} else if ("Windows".equals(operatingSystem)) {
shutdownCommand = "shutdown.exe -s -t 0";
} else {
throw new RuntimeException("Unsupported operating system.");
}
Runtime.getRuntime().exec(shutdownCommand);
System.exit(0);
}
}
Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts
Tuesday, August 9, 2011
Shutdown Computer - Cross Platform
Program to Shutdown Machine.
Play an Audio File
Play an audio file from java program.
import java.io.FileInputStream;
import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class PlaySoundFile {
/** Plays audio from given file names. */
public static void main(String[] args) throws Exception {
String fileName = "D:/songs/laila.wav";
InputStream in = new FileInputStream(fileName);
// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);
// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);
// Similarly, to stop the audio.
// AudioPlayer.player.stop(as);
} // playAudioStream
}
import java.io.FileInputStream;
import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class PlaySoundFile {
/** Plays audio from given file names. */
public static void main(String[] args) throws Exception {
String fileName = "D:/songs/laila.wav";
InputStream in = new FileInputStream(fileName);
// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);
// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);
// Similarly, to stop the audio.
// AudioPlayer.player.stop(as);
} // playAudioStream
}
Monday, August 8, 2011
Call an Exe
Call an exe file with parameters from Java.
import java.io.IOException;
public class RunAnExe {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
Process p =null;
try {
p = rt.exec("notepad c:/test.xml");
} catch (IOException e) {
System.out.println("Exe does not exist or access rights issues" + e.getMessage());
}
System.out.println("Process Exit Normally" + p.exitValue());
}
}
import java.io.IOException;
public class RunAnExe {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
Process p =null;
try {
p = rt.exec("notepad c:/test.xml");
} catch (IOException e) {
System.out.println("Exe does not exist or access rights issues" + e.getMessage());
}
System.out.println("Process Exit Normally" + p.exitValue());
}
}
Get System Processor, Memory, Drives
A Program to get your System Information
import java.io.File;
public class GetSystemInfo {
/**
* @param args
*/
public static void main(String[] args) {
/* Total number of processors or cores available to the JVM */
System.out.println("Available processors (cores): "
+ Runtime.getRuntime().availableProcessors());
/* Total amount of free memory available to the JVM */
System.out.println("Free memory (bytes): "
+ Runtime.getRuntime().freeMemory());
/* This will return Long.MAX_VALUE if there is no preset limit */
long maxMemory = Runtime.getRuntime().maxMemory();
/* Maximum amount of memory the JVM will attempt to use */
System.out.println("Maximum memory (bytes): "
+ (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
/* Total memory currently in use by the JVM */
System.out.println("Total memory (bytes): "
+ Runtime.getRuntime().totalMemory());
/* Get a list of all filesystem roots on this system */
File[] roots = File.listRoots();
/* For each filesystem root, print some info */
for (File root : roots) {
System.out.println("File system root: " + root.getAbsolutePath());
}
}
}
- Available, Free and Maximum memory
- Processors
- File System ( Number of Drives )
import java.io.File;
public class GetSystemInfo {
/**
* @param args
*/
public static void main(String[] args) {
/* Total number of processors or cores available to the JVM */
System.out.println("Available processors (cores): "
+ Runtime.getRuntime().availableProcessors());
/* Total amount of free memory available to the JVM */
System.out.println("Free memory (bytes): "
+ Runtime.getRuntime().freeMemory());
/* This will return Long.MAX_VALUE if there is no preset limit */
long maxMemory = Runtime.getRuntime().maxMemory();
/* Maximum amount of memory the JVM will attempt to use */
System.out.println("Maximum memory (bytes): "
+ (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
/* Total memory currently in use by the JVM */
System.out.println("Total memory (bytes): "
+ Runtime.getRuntime().totalMemory());
/* Get a list of all filesystem roots on this system */
File[] roots = File.listRoots();
/* For each filesystem root, print some info */
for (File root : roots) {
System.out.println("File system root: " + root.getAbsolutePath());
}
}
}
Subscribe to:
Posts (Atom)