I´m running a java project using gnuplot to generate charts in pdf but I want to save those files in another folder outside my working directory. Is that possible?
I have this for now
Process proc = Runtime.getRuntime().exec("gnuplot test.gp");
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
proc.waitFor();
In gnuplot console check help command line options. There is the option -e.
You need to have the following argument for exec() in Java.
"gnuplot -e myOutput='<YourPDF>' test.gp"
where you have to replace <YourPDF> with your path. Since I do not know Java, the Java-people have to tell you how you get this done.
A minimal gnuplot script would for example look like the following:
set term pdfcairo
set output myOutput
plot sin(x) # or whatever
set output
Related
Question:
I have to call an exe file passing 2 string arguments. The call has to get executed as a different user
I referred few of the links that I got from hints. Couple of them were using powershell, few were using runas examples and so on.
But with powershell also, I am not sure if I will face the issue of "cannot be loaded because running scripts is disabled on this system".
Just because of this reason, I want to try something authentic.
Tried following approaches
Trying to do it through a call with RunAs command is making my life difficult. (called a batch file with RunAs command passing a separate pass.txt or savecred). savecred was ruled out by most people. Other one did not work as expected.
Powershell, has issues wherein, I always get a userid/password popup, even though I run through a Java file.
I also get an error in the command prompt as here below
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodExcept
ion
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
(Sample code attached below)
public class SamplePS {
public static void main(String[] args) throws IOException {
Runtime runtime = Runtime.getRuntime();
String command = "powershell C:\\Users\\Ramu\\Project\\SampleFile.ps1";
//String cmds[] = {"C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "\\c", "SampleFile.ps"};
System.out.println("1");
Process proc = runtime.exec(command);
InputStream is = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
System.out.println("2");
}
}
My ps1 file is as here below
$credential = New-Object System.Management.Automation.PSCredential <<domain>>\<<user id>>, <<password>>
start-process cmd.exe -arg "/k dir" -Credential $credential
What is the best way to handle this scenario?
Please suggest! I am not sure what approach to take.
Powershell or Perl or any other scripting.
Whatever it be, I have to make a call from Java file.
Please suggest!
Note: For time being, I dont mind the password being sent as a plain text.
The exe file is in Windows machine
Thanks!
Ram
I want basically to display images into the console terminal, from a Java main program class. The images should show only in the main terminal console, AND NOT open new image display utility windows (i.e. imagemagick, eog, etc.)
I have tried the following workaround: The following Python code (display_image.py) starts tycat/terminology image display utility that successfully shows an image in the console terminal:
import os
os.system("tycat -g 100x100 /home/user/Dekstop/image.png")
So when I execute that Python snippet everything fine. But, when I process call it from Java, like this way here:
Process proc = Runtime.getRuntime().exec(new String[]{"python","/home/user/Desktop/display_image.py"});
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("Here is the standard error of the command (if any):\n");
String s1 = null;
while ((s1 = stdError.readLine()) != null) {
System.out.println(s1);
}
The process seems hanged and does not show anything. Note, that I run the java class from terminology, still the same issue.
I would appreciate any hint or help that would either solve this problem, OR help me show any image file from Java class inside the terminal console application.
Thanks
I want to run a .exe file from my java code,along with passing few arguments/options to the .exe file.
So basically, I did following:
BufferedReader br = null;
OutputResult out = new OutputResult();
String commandStr= "cmd.exe /C A-B/xyz.exe health -U admin -P admin";
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
out.add(line.trim());
}
NOTE: Here A-B is name of the directory in which xyz.exe is located.
But when the variable out is printed, it actually shows that it has nothing.
So instead of above code I modified it to the following:
BufferedReader bre = null;
OutputResult oute = new OutputResult();
String commandStr= "cmd.exe /C A-B/xyz.exe health -U admin -P admin";
Process p = Runtime.getRuntime().exec(commandStr);
bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ((line = bre.readLine()) != null) {
oute.add(line.trim());
}
Now here when the variable oute is printed, it shows the message,
'A-B' is not recognized as an internal or external command, operable program or batch file.
So my question is that why A-B is not being treated as a directory inside which the actual .exe file resides.
Please resolve the error if any one knows about this problem.
You should use a full path to your target xyz.exe - when you execute cmd.exe like that, it's not relative to the folder of your Java program, it's relative to C:\Windows\System32 therefore it can't see your A-B folder.
So, for example:
cmd.exe /C C:/A-B/xyz.exe health -U admin -P admin
And as #CSK correctly noticed, you can execute your .exe directly, without cmd.exe. For examle:
Process process = new ProcessBuilder("C:/A-B/xyz.exe", "health", "-U", "admin", "-P", "-admin").start();
I know that, for example, Java on Raspbian can't access directories with a space (for example /New Folder). I guess its some problem about how Raspbian considers the ' ' char when creating directory. Possibly, Windows might have the same problem with the '-' char. Can you rename the directory without any such chars and try again?
(I use a Mac so I'm unable to recreate the problem. Another option can be, if this is in fact not a problem to do with chars, to create a shell script from which the exe is executed and instead run this script via Java. I am only advising this because I used this method before without any problems.)
I want to run a .sh file using java. I want a terminal to be opened and then I can execute another commands in the same terminal and finally destroy it.
I already used ProcessBuilder but I could not accomplish this.
My piece of code:
ProcessBuilder pb = new ProcessBuilder("/home/omar/ros_ws/baxter2.sh");
Process p = pb.start();
This method used to work in another code, but I don't know why it's not working in mine.
Thanks in advance
How do you know that it doesn't execute? Maybe you just aren't seeing its result. You should get p.getInputStream() after executing and print in your console, like:
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
Also if you're using jdk 7+, try:
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process p = pb.start();
Does your program output an error, or is your program not interacting with the file?
I would suggest trying the directory method within ProcessBuilder.
Process p = null;
ProcessBuilder pb = new ProcessBuilder("baxter2.sh");
pb.directory("/home/omar/ros_ws");
p = pb.start();
If this doesn't work, you should also look into user permissions for the file that you're trying to access.
I think you should grant the .sh file the executable permission to the OS user used to run the java program by using the below command.
chmod u+x baxter2.sh
Using Java's ProcessBuilder, I can run an external script, and redirect its output into my GUI.
Process proc = pb.start();
BufferedReader bri = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while (proc.isAlive())
{
// bri may be empty or incomplete.
while ((line = bri.readLine()) != null)
{
textArea.appendText(line);
}
}
Now, the script I am running also calls other scripts and processes. Two of these, that should be captured, are currently displayed in their own xterm windows. Is it possible to also capture these outputs, and display in a similar manner?.
If these outputs are being managed by your source script, I think it will work. Just as an advice, take a look in this article: When Runtime.exec() won't
It is extremammly important to read it to learn how to work with external processes correctly.