powercfg in java program no error no output - java

powercfg in java program no error no output, when i run it does not shows any output
import java.net.*;
import java.io.*;
public class ip{
public static void main(String args[]) throws IOException{
try{
String inputLine;
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd.exe /c powercfg/batteryreport");
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
//pingResult += inputLine;
}
}
catch(Exception e){
System.out.println(e);
}
}
}

This is because at least on Windows 7, if you try to run powercfg/batteryreport from cmd, you will get
"Invalid Parameters -- try "/?" for help", if you are NOT on laptop. To see this message from Java you should attach p.getErrorStream()
Try 'powercfg -energy', for example

Related

how to run Run a linux command(curl) through java and to get the xml response

I want to execute a Linux command (curl) using java code
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class ExecuteShellCommand {
public String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
public static void main(String args[]) {
ExecuteShellCommand com = new ExecuteShellCommand();
System.out.println(com.executeCommand(
"curl -u '<username><pw>' -k <host>/services/search/jobs -d search=\"abc""));
System.out.println("hello");
The console output is printing hello only with no errors though when i tried the command in git bash it will give a xml response.

cannot print webpage contents to a file in my local system using java

I cannot print contents of a webpage to a file in my local system.please help me to solve this problem
import java.net.*;
import java.io.*;
public class bpart
{
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
OutputStream os = new FileOutputStream("my file location");
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
I would suggest using a try-with-resources structure for you I/O and take a look a java-naming conventions. If you want to write the output of your http-call to a file you can do the following:
public static void main(String[] args) throws Exception {
URL googleUrl = new URL("http://www.google.com/");
try (BufferedReader in = new BufferedReader(new
InputStreamReader(googleUrl.openStream()));
PrintWriter out = new PrintWriter(new FileWriter("path/to/desired/file"))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
out.println(inputLine);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

How to answer a command from the Terminal in Java

I want to execute a command from Java but this command wants me to give it a username.
Like this example:
$ my command
[command] Username:_
So how can I give the command my username in Java?
Currently my code is like this:
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
while ((s = br.readLine()) != null){
System.out.println(s);
}
br.close();
p.waitFor();
p.destroy();
You need to create another thread and pump user input through process#getOutputStream(). See the following example:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class ProcessWithInput {
public static void main(String[] args) throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec("cat");
OutputStream os = p.getOutputStream();
os.write("Hello World".getBytes());
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
br.close();
p.waitFor();
p.destroy();
}
}
Ofcourse, you need to do proper error/exception handling etc.
Other option is to use ProcessBuilder which allows you to provide input through a File.

How do I open any installed software in my pc using a java program?

I want to open installed softwares in my pc using a java program. For Example- If I want to open Microsoft Outlook using java program, how would I do it? Thanks in advance!!
You can use Java ProcessBuilder to launch any program.
Example from this site
package com.javacodegeeks.process;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ProcessBuilderExample {
public static void main(String[] args) throws InterruptedException,
IOException {
ProcessBuilder pb = new ProcessBuilder("echo", "This is ProcessBuilder Example from JCG");
System.out.println("Run echo command");
Process process = pb.start();
int errCode = process.waitFor();
System.out.println("Echo command executed, any errors? " + (errCode == 0 ? "No" : "Yes"));
System.out.println("Echo Output:\n" + output(process.getInputStream()));
}
private static String output(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + System.getProperty("line.separator"));
}
} finally {
br.close();
}
return sb.toString();
}
}

networked computer names to be displayed in jlist

How to retrieve or gathered all computer names from a networked place ? I need some guide or sample code on how to start from scratch.
in simplest form run loop over ip range execute command nslookup
import java.io.*;
public class TestExec {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("nslookup xx.xx.xx.xx ");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
and parse response

Categories