Can I execute a node.js script from a Java Class? - java

I am currently developing a Java system that communicates with node.js using socket.io. The system and the script are on the same server. How can I execute the script from my Java code and keep it alive from my app?

Note when using process builder the path to your JavaScript file is an argument and "node" is the command, so they need to be separated:
ProcessBuilder pb = new ProcessBuilder("node", "app.js");
This is also useful for inheriting its console output, starting the process and getting the reference to the process:
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();

Say you have a node.js script named "mynodejs.js". You can use the following java code:
Process p = Runtime.getRuntime().exec("node mynodejs.js");
or
ProcessBuilder b = new ProcessBuilder("node mynodejs.js", "-args");

Have a look at ProcessBuilder class.
You can start any process on your machine like you would via shell, if I understand your question correctly.

Related

How to run mongod process in background using Java Process Builder?

I'm trying to run a mongod process on a Linux host using Java ProcessBuilder API in a Spring Boot Application running on the same host.
I tried the following ways.
Method 1 : Directly running mongod in bash
String confpath;
String []commands = {"./mongod","-f",confPath,"&"};
ProcessBuilder processBuilder = new ProcessBuilder(commands)
Process process = processBuilder.start();
Running a process with the above array makes ProcessBuilder take & as a mongod parameter and gives an error that & is not an option.
Method 2 : Using bash -c "command"
String exec = String.format("./mongod -f %s",confPath)
String []commands = {"/bin/bash","-c",String.format("\"%s\"",exec),"&"}
ProcessBuilder processBuilder = new ProcessBuilder(commands)
Process process = processBuilder.start();
This method exits with code 127 which means that this command wasn't found.
I tried using absolute paths as well, Any insights would be helpful.
I'm running a JAR file if that helps.

Run a bash script with "source XXXX.sh" by java [duplicate]

I am having a bash script file which I am calling using the source command in a shell and is setting a number of environment variables. Then I can use all the tools the environment variables are setting.
Now I want to do the same in Java by the use of:
static Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();
I know that source is an internal command and I can not call it from Java.
Is there any other way to set the enviroment variable in that file from java in order to be able to use them in my code later for calling other commands?
Thank you in advance!
Process pr = new ProcessBuilder("/bin/bash", "-c", ". env.sh; " + command).start();
Try something like this, where you both source the script and execute a subsequent command in the same shell process. Effectively you source the script every time you want to execute a command.

Running a shell command in a specific directory in java

I'm currently developing a little software in Java and I'm facing a problem I'm not able to solve. In a few words, I am on ArchLinux and I need to run "makepkg" in a specific directory. Of course I tried with
Runtime.getRuntime().exec("cd foo && makepkg");
But I discovered that I cannot cd in directories. Someone has an idea on how to do this? Thanks anyway
A process executor is not a shell. It's done for launching a process.
A thing that can help you is to launch the process from a specified directory.
You can create a ProcessBuilder instance and set the working directory.
It is my way of doing.
ProcessBuilder pb = new ProcessBuilder("makepkg");
pb.directory(new File("foo"));
final Process process = pb.start();
// then you read the flow with process.getInputStream() for example

How to fire command in Ubuntu Terminal using Java code

How to select a directory and fire a command via Terminal using java code in Ubuntu.
For example i want to select the directory of tomcat like "cd /home/sree/tomcat/bin" and fire command like "sh shutdown.sh" and "sh startup.sh" for accessing sh files using java coding.
Also need help for the above process in windows operating system.
Please any one give me a solution. Thanks in advance
You could use Apache Commons CLI to create a program that calls the desired commands you want (you would need to create a version for both Ubuntu and Windows). It offers a lot of flexibility and possibility to plugin to the system pipelines.
After that, you package your program as a jar and run it from the directory you need.
Use the following function:
java.lang.Runtime.getRuntime().exec("a-command");
also, this post may help you:
http://blog.art-of-coding.eu/executing-operating-system-commands-from-java/
There are two classes for this,
java.lang.Runtime
More details
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
java.lang.ProcessBuilder
More details
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
ProcessBuilder in Java
I have used process builder, for that you need to create an instance of ProcessBuilder first and call the start method on it, pass the command that you want to execute as constructor arguement.
ProcessBuilder pb = new ProcessBuilder("ls");
Process p = pb.start();
p.destroy();

How to run bash script from Java

I've seen many similar posts but I am still stumped. I want to run this shell script with a Java program :
C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh
Here's what the code looks like:
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh");
Process p = pb.start();
I am trying to use Process Builder but I'm not sure how to get it to work - I keep getting the error:
Cannot run program "C:\Users\pro-services\Desktop\projects\github\cygwin\TEST.sh": CreateProcess error=193, %1 is not a valid Win32 application
I know there are other fields of ProcessBuilder that I'm not using. I also know that there may be syntax issues here. I wrote my script for Cygwin. Any help please?
Instead of passing the script as an executable to ProcessBuilder, pass in the path to bash binary and pass your script as argument:
ProcessBuilder pb = new ProcessBuilder("C:\\path\\to\\bash.exe", "C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh");
Process p = pb.start();

Categories

Resources