running build.xml from java program - java

i am trying to run ant script from java program.what is the procedure to execute the program
How to run build.xml from java program?
here is how iam trying to implement
Process proc = rt.exec("ant -buildfile D:ant\\trail");
regards,
techie

Check here Execute Ant From Your Application
and look at this example:
Project project = new Project();
project.init();
DefaultLogger logger = new DefaultLogger();
logger.setMessageOutputLevel(Project.MSG_INFO);
logger.setErrorPrintStream(System.err);
logger.setOutputPrintStream(System.out);
project.addBuildListener(logger);
File buildFile = new File("buildhtml.xml");
ProjectHelper.configureProject(project, buildFile);
project.setProperty("ant.file", buildFile.getAbsolutePath());
project.setProperty("item", "ant");
project.setProperty("inputdir", "src/items/ant");
project.setProperty("outputdir", "build/items/ant");
project.setProperty("graphics.prefix", "../../");
try {
project.executeTarget("checkifuptodate");
} catch(Exception e) {System.err.println(e.getMessage());}
// rest of program goes here
It is a better solution than calling Runtime.exec

Rather than trying to start a windows executable separately, it'd be a more robust and flexible solution to use the Ant API. Docs are included with ant itself, they are not online...

Try Runtime.getRuntime().exec("cmd /c start ant.bat");
taken from How do I run a batch file from my Java Application?

Related

Execute R Script from Web Application with Java

I have created a Web Application with Struts, and I would like to execute and R script from a Java class.
My script is in the directory: WebContent/script
The name is: rScript.R
I have checked that a way to execute is using this:
Runtime.getRuntime().exec("WebContent/script/rScript.R")
But this doesn't work. Does someone know how I have to put the sentence?
Cheers.
Try
Process process =Runtime.getRuntime().exec("RScript C:/.../WebContent/script/rScript.R")
process.waitFor();

Get working directory of another Java process

I can get working directory of current Java program using this code:
Path path = Paths.get(*ClassName*.class.getProtectionDomain().getCodeSource().getLocation().toURI());
Also I can get CommandLine parameters (but there is no directory in the output) of running Java processes using this command wmic process get CommandLine where name='java.exe' /value
It is possible to get working directory of another Java process (better programmatically)? Probably it can be solved with some jdk/bin utilities?
You can get this information via the Attach API. To use it, you have to add the tools.jar of your jdk to your class path. Then, the following code will print the current working directories of all recognized JVM processes:
for(VirtualMachineDescriptor d: VirtualMachine.list()) {
System.out.println(d.id()+"\t"+d.displayName());
try {
VirtualMachine vm = VirtualMachine.attach(d);
try(Closeable c = vm::detach) {
System.out.println("\tcurrent dir: "+vm.getSystemProperties().get("user.dir"));
}
}
catch(AttachNotSupportedException|IOException ex) {
System.out.println("\t"+ex);
}
}

Run process through java program

I am working in java for Ubuntu environment. I want to execute a script file but through java program. I have tried followings
try{
Runtime.getRuntime().exec("/usr/lib/flume-ng/bin/flume-ng agent -f /home/c4/Flume/New/ClientAgent.config -n clientAgent");
}
catch(Exception e){
e.printStackTrace();
}
And
ProcessBuilder pb = new ProcessBuilder("/usr/lib/flume-ng/bin/flume-ng", "agent",
"-f", "/home/c4/Flume/New/ClientAgent.config","-n","clientAgent");
try{
pb.start();
}
catch(Exception e){
e.printStackTrace();
}
But both of these don't start my process named: flume-ng, other are parameters passed to it. Java program compiles successfully but neither gives exception etc nor run process. Please guide me if i am doing something wrong.
Consider Apache Commons Exec
It provides reliable and cross-platform mechanism to launch external processes.
Here is the latest javadoc: http://commons.apache.org/proper/commons-exec/apidocs/index.html
And here is the simple tutorial: http://commons.apache.org/proper/commons-exec/tutorial.html

CCNx Java Code Help (ProcessBuilder)

Has anyone played around with the CCNx code over from http://www.ccnx.org/.
I unzip the project and loading the .project file in the javasrc directory into Eclipse. The project builds with no errors.
I'm guess I need to start the CCNDaemon (org.ccnx.ccn.impl.support.CCNDaemon) but I get an error at:
java.io.IOException: Cannot run program "../ccnd/agent/ccnd": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at org.ccnx.ccn.impl.support.CCNDaemon$CCNDWorkerThread.initialize(CCNDaemon.java:93)
at org.ccnx.ccn.impl.support.Daemon$WorkerThread.run(Daemon.java:125)
It looks like its trying to build a new process with
private static final String DEFAULT_CCND_COMMAND_STRING = "../ccnd/agent/ccnd";
protected String _command = DEFAULT_CCND_COMMAND_STRING;
...
ProcessBuilder pb = new ProcessBuilder(_command);
I don't have the ccnd operating system process. Do I need to build the C++ code? Or is there some way to run this with pure java? Thanks for the help!
Based on my findings, it turns out that all CCN applications require a CCNx Daemon. This is only based in C right now. So you have to build the C code with all the dependencies. The Java code actually calls the ccnd (CCN Daemon) to run with the ProcessBuilder.
I wrote up a blog post about how I got it to work for Ubuntu... but its basically.
C Source Dependencies:
sudo apt-get install git-core python-dev libssl-dev libpcap-dev libexpat1-dev athena-jot
Run:
./configure
Build CCN with:
make
Test with:
make test
Start the ccnd:
ccndstart
The blog post has more details.
If you add -start in the arguments block in the "Run configuration" dialog in Eclipse before you run the CCNDaemon, there should be no errors.
————————————————————————————————————————————
2011-10-5 19:49:39 org.ccnx.ccn.impl.support.Daemon startDaemon
信息: Starting daemon with command line: java -Djava.library.path=.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java -cp /Users/thomas/Downloads/ccnx-0.4.1/javasrc /bin:/Applications/eclipse/plugins/org.junit_4.8.2.v4_8_2_v20110321-1705/junit.jar:/Applications/eclipse/plugins/org.hamcrest.core_1.1.0.v20090501071000.jar:/Users/thomas/Downloads/ccnx-0.4.1/javasrc/lib/bcprov-jdk16-143.jar:/Users/thomas/Downloads/ccnx-0.4.1/javasrc/lib/junit-4.3.1.jar:/Users/thomas/Downloads/ccnx-0.4.1/javasrc/lib/kxml2-2.3.0.jar org.ccnx.ccn.impl.support.CCNDaemon -daemon
Started daemon ccnd. PID 3127
2011-10-5 19:49:40 org.ccnx.ccn.impl.support.Daemon startDaemon
信息: Started daemon ccnd. PID 3127
——————————————————————————————————————————————————————————————————————————
(blank lines introduced for clarity)

IntelliJ Plugin - Run Console Command

I am new to plugin development for IntelliJ and would like to know, how I can execute a command in the command line from within my plugin.
I would like to call, for instance, the command "gulp" in the current projects root directory.
I already tried using
Runtime.getRuntime().exec(commands);
with commands like "cd C:\Users\User\MyProject" and "gulp", but it does not seem to work that way and I wonder, if the plugin API provides an easier method.
I know its a bit late (1 year later), but recently I was working on an IntelliJ plugin and I had the same issue and this is what I used and it works pretty well.
First, we need to create a list of commands that we need to execute:
ArrayList<String> cmds = new ArrayList<>();
cmds.add("./gradlew");
Then
GeneralCommandLine generalCommandLine = new GeneralCommandLine(cmds);
generalCommandLine.setCharset(Charset.forName("UTF-8"));
generalCommandLine.setWorkDirectory(project.getBasePath());
ProcessHandler processHandler = new OSProcessHandler(generalCommandLine);
processHandler.startNotify();
hence the generalCommandLine.setWorkDirectory is set to the project directory which could be equivalent to the terminal command cd path/to/dir/
The Runtime class provides exec(String[], String[], File) method where the last argument is working directory of the subprocess being launched.
The plugin API provides OSProcessHandler class (as well as other classes like ProcessAdapter) which can help to manage the subprocess, handle its output etc.
ProcessOutput result1 = ExecUtil.execAndGetOutput(generalCommandLine);
result1.getStdOut result1.getStdErr works
and
ScriptRunnerUtil.getProcessOutput(generalCommandLine, ScriptRunnerUtil.STDOUT_OUTPUT_KEY_FILTER, timeout);
both work pretty well
and they are built into intellij
import com.intellij.execution.process.ScriptRunnerUtil;
import com.intellij.execution.util.ExecUtil;

Categories