Run sbt build from IntelliJ IDEA plugin - java

I develop IntelliJ IDEA plugin which adds a button "Build my project" in the Main menu. When user click the button, the plugin should start build of SBT project and put SBT output to console, so that user will see the build progress. To run the build I need 3 actions:
cd to project directory.
execute "sbt package" command.
print command output to the console.
Here is my code:
`
Runtime r = Runtime.getRuntime();
Process p = r.exec("cd /project_dir && sbt package");
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null){
MyIdeaView.getInstance(project).getConsole().print(line);
}
`
The problem is that when I pass couple of commands, separated by "&&" to Runtime.exec(), I don't see any output.
I tried the same with another couple of commands:
Process p = r.exec("cd /project_dir && pwd");
and I still don't see any output, so the problem is not in sbt command. When I pass single command, e.g. "pwd" or "ls" to Runtime.exec() method, I successfully see the command output.
So, can anybody suggest, how to run both commands "cd" and "sbt package" from IDEA plugin and get the output of "sbt package" command?

Use
Process Builder Class to execute list of commands
ProcessBuilder(List <String> command)
This constructs a process builder with the specified operating system
program and arguments.
Builder processBuilder = new ProcessBuilder("cd /project_dir","sbt package");
Process process = processBuilder.start();

Related

Unable to create a folder by executing a shell script using java

I was trying to execute shell scripts using java code. The following is a sample code to demonstrate the issue :
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("/home/otaku/Programming/data/test1.sh");
try {
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println(output);
} else {
System.out.println("Script exited abnormally");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
The shell script file test1.sh that I am trying to execute :
#!/bin/bash
mkdir -p -- teeh
echo 'Succesfully executed script'
I am getting echo message and was able to print in the java console indicating that the shell script is executed successfully. But no directory is being created even though the command mkdir -p -- teeh is executed. If I manually execute the script file using terminal it works like a charm. I would like to know the reason behind this and a possible solution to this as well.
mkdir -p -- teeh
In this command the teeh path is a relative one rather than an absolute one: it will be created in the script's current working directory.
Your bash script is by default executed with the working directory of your JVM, which depends on where you executed your java application from. If you're executing your code from your IDE, by default this will be the project's root directory. If you're executing from the command line, it will be the directory you execute the java command from.
In any case you shouldn't expect a /home/otaku/Programming/data/teeh directory to be created by your current code unless you run the java application from the /home/otaku/Programming/data/ directory.
There are many possible solutions, whose relevance depend on your context :
execute your java code from the /home/otaku/Programming/data/ directory
use an absolute path in your bash script
use cd in your bash script
use ProcessBuilder.directory(File dir) to execute the bash script with the appropriate working directory

ProcessBuilder cannot find python3 in Mac OS

I am trying to execute "python3 --version" (this is just an example) from Java using ProcessBuilder. python3 is located in /usr/local/bin. I have configured the working directory. Here is my code snippet :
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "python3 --version");
pb.directory(new File("/usr/local/bin"));
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
But it gives the error : /bin/bash: python3: command not found. Any way to resolve this?
PS : It can execute python --version as it is located in /usr/bin. Rather it executes successfully all commands pertaining to /usr/bin but none of the ones located in /usr/local/bin. python3 is just an instance of the general problem I am facing.
We also have to configure the environment (more so the PATH variable) and append /usr/local/bin as well to it. It will work fine then. I use the Eclipse IDE and I configured the PATH under environment in Run Configurations. It works fine now.

Java Shell Command not Running from Current Directory

In this question it shows that when you run a shell command from Java, it runs from the current directory. When I run the command javac Program.java from my program, it shows the error (from the standard error stream):
javac: file not found: Program.java
Usage: javac <options> <source files>
use -help for a list of possible options
However, when I run the same exact command from the actual Terminal, it works fine and saves the .class file in the default directory. This is the code:
Runtime rt = Runtime.getRuntime();
String command = "javac Program.java";
Process proc = rt.exec(command);
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);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
proc.waitFor();
Any ideas why it works when I type it in the actual Terminal, but not when I run it from my program? I am running a Max OS X Mountain Lion (10.6)
Thanks
You can try to print the path in your programm ,use new File(".").getAbsolutePath() ,if you are in a ide ,the path may be in the root of the project rather than in the path of the current java file
Your program is probably being run from a different directory. Your IDE (such as Eclipse) is probably is running from one location, and knows the directory structure to access your program files.
The easiest, quickest solution is to just write the fully-qualified file path for Program.java.
The alternate is to find out what the current directory is. So, perhaps run pwd the same way you are running javac Program.java from within your program's code? Then you can see what directory your program is actually being run from. Once you know that, you can write the appropriate directory structure.
For example, if pwd reveals that you are actually 2 directories above where Program.java is, then you can put those directories in the command like this: javac ./dir1/dir2/Program.java.
To change the directory Eclipse runs from, see this question Set the execution directory in Eclipse?
The reason my code was not working was because I was running it in Eclipse IDE, and that messes up the directory the program is running from. To fix that program, I changed the command to javac -d . src/Program.java. If I export the program into a .jar file and run it in the desktop, my original command will do just fine.
Thanks to saka1029 for helping me!

Java: execute command in a new cmd.exe as administrator

I want to execute a command from a Java-application using
Runtime.getRuntime.exec(command);
but the command need Admin-privileges. If I use
runas /user:Administrator "cmdName parameters"
nothing happens because I need to give user und pw as parameter to the command.
But I need to run a command to the cmd, so that a new cmd.exe starts as administrator and asks if I want to run cmd.exe as admin. After agree the command should be run in the admin-cmd. So like this:
String command = "popupNewCmdAsAdminAndRun "batWhichNeedsAdmin.bat" "
Runtime.getRuntime.exec(command);
Has anyone an Idea?
Thanks in advance!
you should do the following
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "command";
process.StartInfo.Verb = "runas";
process.Start();
process.WaitForExit(60000);
Start Command Prompt (CMD) from java as Administrator and Run command prompt as Administrator - or the silly way, try to pipe it:
pw | command
Research building projects and the executable jar file. Usually when you build a project, it gives you a commandline that can be used to execute the jar file in folder dist located in the projects folder. You can copy and paste the command line in cmd. But you could also use a bat file, or convert the executable jar file into an exe with the jar2exe converter. Not completely sure of the name because i am not posting on my computer.

Running program with subprocesses with Java

I need to run an external program written in C from my Java application.
I'm trying to use Runtime.getRuntime().exec() with partial success. I execute program using String with path to .exe file and its arguments:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
log.info(line);
}
int exitVal = pr.waitFor();
This would work just fine - program executes and sends information about its behavior to console. Problem is that external program tries to run other subprograms during its execution. So basically what I need to do is: I run program.exe with my Java application and program.exe runs subprogram.exe. Unfortunately, that's not the case, because subprogram.exe never starts in current situation.
What should I do differently to make it work ? Thanks for any help.
It is not my decision to have it so complicated and I would be more than happy to have just one .exe file to execute, but I can't.
Try to run it from cmd.exe i.e.the command prompt and see if its working well.The console output should give you a trace of the program execution.

Categories