package com.studytrails.tutorials.springremotingrmiserver;
import java.lang.Object;
import java.awt.Desktop;
import java.io.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class GreetingServiceImpl implements GreetingService
{
#Override
public String getGreeting(String name) {
return "Hello " + name + "!";
}
public String getText() {
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-server.xml"});
Resource resource = appContext.getResource("file:D:\\text\\test.txt");
StringBuilder builder = new StringBuilder();
try {
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
File temp=File.createTempFile("output", ".tmp");
String filePath=temp.getAbsolutePath();
System.out.println(""+filePath);
String tem=temp.getName();
String line;
PrintWriter out = new PrintWriter(new FileWriter(tem));
//System.out.println(""+filePath);
while ((line = br.readLine()) != null) {
out.println(line);
}
temp.setReadOnly();
String[] cmd = {"notepad",tem};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
// proc.getInputStream();
out.close();
br.close();
//temp.deleteOnExit();
}
catch(IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
In above code I am not able to setReadonly(); command to temp file. File displayed with all options can you suggest how to make the temp file as not modified and even it is not able to save to another location. I need this file only displayed at run time of the program. During that time user does not change any content and it could not be save as another location.
I suggest to close the file before using it (by notepad):
PrintWriter out = new PrintWriter(new FileWriter(tem));
//System.out.println(""+filePath);
while ((line = br.readLine()) != null) {
out.println(line);
}
out.close();
temp.setReadOnly();
String[] cmd = {"notepad",tem};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
// proc.getInputStream();
To avoid that a user move the file to another location you may additional create the temp file in a temp folder and make the folder readonly too. But if user can read they normally can copy the file to another location. If I remember well, in Windows you can mark for read but prevent from copy a file (thru Windows of course).
Related
A very simple code running in the debugging mode perfectly but not working after installation of exe, giving no response/result in return. even no errors to trace.
After building a .exe and installing on my PC its happening, very strange.
tried process builder but the same thing, anyway to check/trace it. maybe paths ?
StringBuilder b = new StringBuilder();
Process p = Runtime.getRuntime().exec("wmic diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
Please note: CMD /c before commands also return an empty response in actual env.
An internal windows command with arguments, like "wmic diskdrive ..."
can be executed easily by wrapping it up inside a cmd window.
Here is the working code snippet for running the wmic command encapsulated in a cmd window:
import java.io.*;
public class Wmic {
public static void main(String[] args) {
StringBuilder b = new StringBuilder();
try {
// Initialize a process object to run
// wmic command and its parameters
// inside a cmd window
Process process = Runtime.getRuntime()
.exec("cmd /c C:\\Windows\\System32\\wbem\\WMIC.exe diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
} catch (Exception ex) {
b.append(ex.toString());
}
System.out.println("Output: \n" + b.toString());
}
}
Output:
>javac Wmic.java
>java Wmic
Output:
Signature
More information:
https://mkyong.com/java/how-to-execute-shell-command-from-java/
The ProcessBuilder constructor takes a list of strings. When using ProcessBuilder to run a command, I separate all the words in the command into separate strings.
I read the output of the process in a separate thread. And I always wait for the command, that I launched via ProcessBuilder, to terminate.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
public class PrcBldTs {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("wmic","diskdrive","get","signature");
try {
Process proc = pb.start();
StreamGobbler error = new StreamGobbler(proc.getErrorStream());
StreamGobbler output = new StreamGobbler(proc.getInputStream());
Thread stdout = new Thread(output);
Thread stderr = new Thread(error);
stdout.start();
stderr.start();
int result = proc.waitFor();
stdout.join();
stderr.join();
System.out.println("Exit status = " + result);
if (result != 0) {
System.out.println(error.getContents());
}
else {
System.out.println(output.getContents());
}
}
catch (IOException | InterruptedException x) {
x.printStackTrace();
}
}
}
class StreamGobbler implements Runnable {
private BufferedReader reader;
private StringWriter sw;
public StreamGobbler(InputStream is) {
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
sw = new StringWriter();
}
public String getContents() {
return sw.toString();
}
public void run() {
try {
String line = reader.readLine();
while (line != null) {
sw.append(line);
sw.append(System.lineSeparator());
line = reader.readLine();
}
}
catch (IOException xIo) {
throw new RuntimeException(xIo);
}
}
}
Running the above code gave me the following output.
Exit status = 0
Signature
1145609371
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ExecuteShellComand {
public static void main(String[] args) {
ExecuteShellComand obj = new ExecuteShellComand();
String className = "str.java";
String command = "javac " + className;
String output = obj.executeCommand(command);
System.out.println(output);// prints the output of the executed command
}
private 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();
}
}
I am trying to compile a Java file (str.java) from another Java class(ExecuteShellComand.java). What I am trying to do is if "str.java" compiles successfully then I want to execute "java str" command, but if the compilation fails then proper stacktrace or errors should be printed. I am storing the stacktrace or the errors in output variable.
But when I execute this code although "str.java" has somes errors in it System.out.println(output) is not printing the errors.
If you want to capture the errors from a command then you shall capture error stream instead of Input stream
So replace
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
with
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
The Process class tries to mimetize OS process. It means, process keep different output stream for error and normal messages and one stream for input. In UNIX, should be:
wc < file > wc.count 2> wc.error
In Java...
abstract InputStream getErrorStream()
Gets the error stream of the subprocess.
abstract InputStream getInputStream()
Gets the input stream of the subprocess.
abstract OutputStream getOutputStream()
So, you should use getErrorStream() to get errors..
Refactoring your code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ExecuteShellComand {
public static void main(String[] args) {
ExecuteShellComand obj = new ExecuteShellComand();
String className = "str.java";
String command = "javac " + className;
obj.executeCommand(command);
System.out.println(obj.output);
System.out.println(obj.errors);
}
private String errors;
private String output;
private void executeCommand(String command) {
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
errors = readStream(p.getErrorStream());
output = readStream(p.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
private String readStream(InputStream inputStream) throws IOException {
StringBuffer output = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
return output.toString();
}
}
package com.studytrails.tutorials.springremotingrmiserver;
import java.lang.Object;
import java.awt.Desktop;
import java.io.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class GreetingServiceImpl implements GreetingService {
#Override
public String getGreeting(String name) {
return "Hello " + name + "!";
}
public String getText()
{
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-server.xml"});
Resource resource = appContext.getResource("file:D:\\text\\test.txt");
StringBuilder builder = new StringBuilder();
try{
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
File temp=File.createTempFile("output", ".tmp");
String filePath=temp.getAbsolutePath();
System.out.println(""+filePath);
String line;
PrintWriter out = new PrintWriter(new FileWriter(temp));
//System.out.println(""+filePath);
while ((line = br.readLine()) != null) {
out.println(line);
}
String tem=temp.getName();
//temp.setReadOnly();
String[] cmd = {"notepad",tem};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
out.close();
br.close();
temp.deleteOnExit();
}catch(IOException e){
e.printStackTrace();
}
return builder.toString();
}
}
In the above code creates the temporary file but i am trying to write contents in that file but when the program executed notepad is open with the temporary name but it gives message file not exists do you want to create. i need that temporary file with the text present in the D:\text\test.txt location. kindly suggest me
Ok here is the updated code that should work. I have writen it as a java program but it is working. Your main problem was you were calling temp.deleteOnExit(); just after the process open. You have to wait for the process to complete, other wise the main thread will delete the file even before the notepad opens it. Also use out.write(line); Hope it helps.
String line;
PrintWriter out = new PrintWriter(new FileWriter(temp));
while ((line = br.readLine()) != null) {
out.write(line);
}
out.flush();
out.close();
br.close();
String[] cmd = { "notepad", filePath };
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
out.close();
br.close();
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
temp.deleteOnExit();
I can't figure out for the life of me what is wrong with this program:
import java.io.*;
public class EncyptionAssignment
{
public static void main (String[] args) throws IOException
{
String line;
BufferedReader in;
in = new BufferedReader(new FileReader("notepad encypt.me.txt"));
line = in.readLine();
while(line != null)
{
System.out.println(line);
line = in.readLine();
}
System.out.println(line);
}
}
The error message says that the file can't be found, but I know that the file already exists. Do I need to save the file in a special folder?
The error is "notepad encypt.me.txt".
Since your file is named "encypt.me.txt", you can't put a "notepad" in front of its name. Moreover, the file named "notepad encypt.me.txt" probably didn't exist or is not the one that you want to open.
Additionally, you have to provide the path ( absolute or relative ) of your file if it's not located in your project folder.
I will take the hypothesis that your are on a Microsoft Windows system.
If your file has as absolute path of "C:\foo\bar\encypt.me.txt", you will have to pass it as "C:\\foo\\bar\\encypt.me.txt" or as "C:"+File.separatorChar+"foo"+File.separatorChar+"bar"+File.separatorChar+encypt.me.txt".
If it's still not working, you should verify that the file :
1) Exist at the path provided.
You can do it by using the following piece of code:
File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt");
System.out.println(encyptFile.exists());
If the path provided is the right one, it should be at true.
2) Can be read by the application
You can do it by using the following piece of code:
File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt");
System.out.println(encyptFile.canRead());
If you have the permission to read the file, it should be at true.
More informations:
Javadoc of File
Informations about Path in computing
import java.io.*;
public class Test {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "temp.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
package com.mkyong.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Reference: http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
I have a UNIX native executable that requires the arguments to be fed in like this
prog.exe < foo.txt.
foo.txt has two lines:
bar
baz
I am using java.lang.ProcessBuilder to execute this command. Unfortunately, prog.exe will only work using the redirect from a file. Is there some way I can mimic this behavior in Java?
Of course,
ProcessBuilder pb = new ProcessBuilder("prog.exe", "bar", "baz");
does not work.
Thanks!
ProcessBuilder pb = new ProcessBuilder("prog.exe");
Process p = pb.start();
OutputStream pos = p.getOutputStream();
InputStream fis = new FileInputStream("file.txt");
byte[] buffer = new byte[1024];
int read = 0;
while((read = fis.read(buffer)) != -1) {
pos.write(buffer, 0, read);
}
fis.close();
Not tested, but something like this should work.
I ended up doing something like this and it works. Thanks for all the help!
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
public class UserInp {
public static void main(String[] args) {
new UserInp().sample();
}
public void sample() {
String command = "foo.exe";
List<String> args = new LinkedList<String>();
args.add("bar");
args.add("baz");
ProcessBuilder pb = new ProcessBuilder(command);
java.lang.Process process = null;
try {
process = pb.start();
} catch (IOException ex) {
//--
}
OutputStream os = process.getOutputStream();
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
final InputStream is = process.getInputStream();
new Thread(new Runnable() {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (java.io.IOException e) {
}
}
}).start();
for (String arg : args) {
pw.println(arg);
}
pw.close();
int returnCode = -1;
try {
returnCode = process.waitFor();
} catch (InterruptedException ex) {
//--
}
System.out.println(returnCode);
}
}
The redirection is setup by the shell you need todo this manually, something like that:
Process proc = pb.start();
OutputStreamWriter os = new OutputStreamWriter(proc.getOutputStream());
// then write the data from your file to os
// ...
os.close();
Did you try to wrap prog.exe into a script which accepts arguments and deal with prog.exe ?
I assume you're using a shell, so it's quite simple to set up a bash script.
If I understand your problem, the script would look like :
#!/usr/bin/bash
echo $1 > file
echo $2 >> file
prog.exe < file
remove file
Build the process using a ProcessBuilder, then use process.getOutputStream() to get an OutputStream that will pipe to the standard input of the process.
Open the file using normal Java techniques, read the contents of the file and write it to the OutputStream going to the Process you made with the ProcessBuilder.
The problem you have right now is that you're calling the ProcessBuilder to launch
$ prog.exe foo bar
Which is probably nothing close to what you want to achieve.