Calling perl from java - java

How do I call a perl script from java? Currently I have this code but a create process error =2 keeps coming up.
Process p = Runtime.getRuntime().exec("perl C:/Users/Mahish/Documents/PLUa/src//test.pl");

You need to set full path to your dir perl\bin
Example command to do this in CMD:
SET PATH=%PATH%;c:\perl\bin
and after that you can use:
Process p = Runtime.getRuntime().exec("perl C:/Users/Mahish/Documents/PLUa/src/test.pl");

Related

Is there a way to have Java call a bash script which calls javac and java?

I'm trying to train a neural network to play Halite 3. The provided interface is a bash script which:
1. compiles my bot
2. calls the binary file with a string to run the bot java myBot
I'm trying to run this script from Java to train the network.
I've tried using a ProcessBuilder to run the script as well as the binary in the script. Running the script produces no output, and using echo I've determined that the program terminates when javac is called in the script. Removing that call, it terminates when the program is run.
I've tried calling the program directly as well using ProcessBuilder, and this does indeed produce output. The issue is it doesn't run the bots properly saying it can't find the file. I've tried changing the path to be relative to different directory levels as well as the absolute path (the java command doesn't seem to like absolute paths?).
Calling the binary directly:
List<String> cmd = new ArrayList<>();
cmd.add(dir+ "/src/halite");
// Replay
cmd.add("--replay-directory");
cmd.add(dir+"/replays/");
// Options
cmd.add("--results-as-json");
cmd.add("--no-logs");
// Dimensions
cmd.add("--width");
cmd.add("16");
cmd.add("--height");
cmd.add("16");
// Players
cmd.add("\"java -cp . myBot\"");
cmd.add("\"java -cp . myBot\"");
Process proc = new ProcessBuilder(cmd).start();
InputStream is = proc.getInputStream();
Scanner s = new Scanner(is);
while (s.hasNext()){
System.out.println((String) s.next());
}
This code does produce a JSON, however, I get an error in my logs saying that the bots do not run.

how to pass parameter to shell script to use as part of variable filename [duplicate]

This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
Closed 4 years ago.
I am trying to programmatically call from my java program a shell script that in turn executes a command depending of the parameter sent.
the command to be executed inside the connectvpn.sh shell script is:
echo myrootpassword | sudo -S /usr/local/Cellar/openvpn/2.3.8/sbin/openvpn --config /usr/local/etc/openvpn/1.opvn
or
echo myrootpassword | sudo -S /usr/local/Cellar/openvpn/2.3.8/sbin/openvpn --config /usr/local/etc/openvpn/2.opvn
and so on from a long list, where the filename number I want to be variable depending on the value of the parameter received
So I want my java program to be able to use always the same shell script but use different .ovpn file depending on the parameter sent.
I believe that on my java program I have to call it something like this:
server_number = 1;
ProcessBuilder pb = new ProcessBuilder("./connectvpn.sh", server_number);
Process proc = pb.start();
What would go in the shell script so that the filename called is variable and for the example shown it uses 1 but other times it uses whatever number is sent as parameter?
Thank you very much!
In the shell script, use $1 to denote the first parameter passed to it.
Fix your java as below,
ProcessBuilder pb = new ProcessBuilder("./connectvpn.sh", String.valueOf(server_number));

get errorlevel in Java

I write the code in Java to launch specific application via command prompt. So the code will open cmd and run the application from there. I want to get the errorlevel of the execution. I tried with
String errorlevel = System.getenv("errorlevel");
System.out.println(errorlevel);
It always returns zero either the execution is failed or not. I also tried with getProperty() and came up with the same result.
Then the other way I tried
Process p = Runtime.getRuntime().exec(command);
int i = p.waitFor();
System.out.println("errorLevel :" + ""+ i);
With this, i get the exitValue but not exactly what I want since I have to close the application to retrieve the value
What I want to know is whether it is possible to get the errorlevel of commandprompt without exit/close the process manually
Thanks before

How to open the terminal in Ubuntu and passe parameters to it by using Java code?

I'm using Ubuntu and I just want to open the terminal from Java and execute this command make iris sim. After that keep the terminal open until the user click any key.
This is my code, but it's not working correctly:
String []commands= {"make", "iris", "sim"};
String[] cmdArray = {"/usr/bin/xterm", "-e"};
Process p = Runtime.getRuntime().exec(cmdArray, commands);
You are using the exec command wrong. The second parameter is not an array of "commands" but an array of environment variables. See the JavaDocs.
Instead, simply call your command:
Process p = Runtime.getRuntime().exec("/usr/bin/xterm -e make iris sim");
Note that this will create a new process. If you want to wait for this process call p.waitFor();.

Hide BAT file window when invoking it from Java

I'm invoking the execution of a BAT file from Java with the Runtime object.
Is it possible to hide the BAT window during the execution of the script? How is it possible?
Try using javaw rather than java to run the script.
http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html
Update: Sorry, I think I read the question wrong. I know I've suppressed a .bat window doing something along these lines before:
http://www.geekstogo.com/forum/topic/56092-hide-the-command-prompt-windows/
Invoke start as the first command in your process builder, with the /b option:
ProcessBuilder builder = new ProcessBuilder("start", "/b", "<mybatchcommand>");
// .. set environment, handle streams
builder.start();
The /b options suppresses the command window.
Process p = Runtime.getRuntime().exec("scriptName.vbs");
In scriptName.vbs you write
var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c abc.bat", WindowStyle_Hidden)

Categories