How to run a exe file via unix by passing input parameter - java

I have created a exe file from jar via converter tools. Jar file was executing fine when I tried to run via unix by passing input parameters eg: java -jar SSS_Infinite.jar test.in 2
However after converting to exe I tried to run by passing input parameters via Unix but its not working and simply returns to the next line. I tried the below command in Unix cmd. Is there any other alternative to make it trigger ?
SSS_Infinite.exe test1.in 2

I assume you created executable for Windows platform, it will not work on *nix systems.
The simplest option will be to build little script that will accept parameters and pass them to java -jar, something like that:
#!/bin/bash
java -jar SSS_Infinite.jar $1 $2
where $1 and $2 are script arguments, see explanation here.
after you create that script and save it as say SSS_Infinite.sh, change its permissions:
chmod +x SSS_Infinite.sh
Then you'll be able to execute it like that:
./SSS_Infinite.sh test1.in 2

Related

Is it possible to embed a jar file in a Windows Powershell script?

When I have a jar(hoge.jar) file, and a unix shell script(hoge.sh) or a windows batch file(hoge.bat), I can obtain ones with the jar file like below:
cat hoge.sh hoge.jar >foo.sh
or
copy /b hoge.bat+hoge.jar foo.bat
Is there any way to do that with a powershell script (hoge.ps1)
copy /b hoge.ps1+hoge.jar foo.ps1
As for the foo.ps1 obtained as above,
java -jar foo.ps1
does work, but the part of the powerscript never runs... (when used as the powerscript ofcourse)
You can potentially store any file in a powershell script. Here is an excellent article that shows you how to do so with base 64 encoding.

How can I execute .jar file from the terminal without specifying its name

How can I execute .jar file in folder from the Windows cmd without specifying its name.
I have tried below command (as there is only 1 jar in that folder I used *) but its not working(Error: Unable to access jarfile *.jar
).
java -jar *.jar
I am not sure it would be a good idea to just run everything in a directory, but you could:
FOR %A IN ("*.jar") DO (java -jar "%~A")
So what you appear to be asking is how to run the command
% java -jar somelongname.jar
as
% java -jar *.jar
to avoid some typing. That's not possible, because neither the Windows CMD shell or the java command is going to expand the *.jar wildcard pattern.
(On Linux / Unix / MacOS, the shell does wildcard expansion before passing arguments to a command. On Windows, it is the responsibility of the command to do this. In practice, it appears that the java command only expands wildcards in the arguments that are going to be passed to your application; see Stop expanding wildcard symbols in command line arguments to Java)
So if you want to avoid typing those pesky characters on Windows, you will need to do something like:
write a simple BAT file to run "java -jar somelongname.jar", or
write a clever BAT file to identify and run a JAR file that matches "*.jar", or
use Powershell.
For what it is worth, I think what you are trying to do is rather dangerous. This is a bit like typing "di*" to run the "dir". What if there is some other more dangerous command on the PATH that is going to match instead of "dir"?

Running java executable with powershell within command prompt

I have a powershell script that is working and runs a java executable. Before I was generating a bunch of powershell script files that were run through the command prompt. Now I want to make it so there does not need to be file creation.
Here is what the line looks like from the working (.ps1) file:
java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe
I want to be able to do something like this in command prompt:
Powershell -Command "java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe"
Even just asking this question I am having problems with the escape characters. What is the proper way to handle escape characters when you have quotes in quotes in quotes when calling java through powershell through command prompt? (I understand it is a bit messy)
You can lessen the quoting headaches if you focus just on the parts that require quoting (assuming that option value true truly needs quoting):
REM From cmd.exe
C:\> powershell -Command java -Doption1=3 -Doption2="'\"true\"'" jar.exe
The above will make java.exe see:
java -Doption1=3 -Doption2="true" jar.exe
As you can see, even this simple case of the desired resultant quoting is quite obscure, because you're dealing with 3 layers of interpretation:
cmd.exe's own interpretation of the initial command line
PowerShell's interpretation of the arguments it receives.
How PowerShell's translates the arguments into a call to an external program (java.exe).
In any event, the final layer of interpretation is how the target program (java.exe) parses the command line.
That said, in order to call java.exe, i.e. an external program, you don't need PowerShell at all; invoke it directly:
REM From cmd.exe
C:\> java -Doption1=3 -Doption2="true" jar.exe

Translation of java commands in Linux systems

I am currently using a linux system to execute a specific command that translates into another java command (java - jar)
For example:
when i try execute /usr/bin/a in terminal, it will read the /usr/bin/a command and translates into 'java -jar' command
I do not want to execute 'java -jar' command directly and i would to specify a full path in executing the command so is there a possible way to achieve this without using a script like /.sh?
Things that i have attempted:
i have tried using the alias command in bashrc files
for example in bashrc file:
alias /usr/bin/a='java - jar'
but when i try to source the bashrc files, it gives me an invalid alias.
I know i can use a /.sh script to execute the command but that is not my intention to do it.
Have you tried to write a bash script with java -jar and execute it ?
You test.sh file will contain:
java -jar test.jar
And after that you can run
./test.sh
And offcourse you can put to bin directory or create an alias for that bash file

Executing mount command in Java on Android

I'm trying to write Java code that executes some terminal commands. The code should execute this command sudo mount -o loop system.img system. But there are several problems. First, to execute this command I have to be root. I know that I can be by sudo su, but how can I stay as root when I close the terminal window? If I use the command sudo mount -o loop system.img system how can I provide the password in the Java code?
The second issue is: can I execute the command as below?
File f2 = new File("/home/user1/Desktop/aDirectory");
String[] commands = new String[]{"sudo mount", "-o", "loop", "/home/user1/Desktop/aDirectory/system.img"};
Runtime.getRuntime().exec(commands, null, f2);
I think I can't. So how can I do it? Any ideas?
Notes: system.img is a compiled Android os file. and the system is an empty directory. The thing I'm trying to do is mount the system.img file into the system directory.
Programs like sudo read the password directly from the terminal device, not from stdin, so this is unfortunately not a trivial thing to do. I'm not sure if this is realistic for Android or not, but on a general UNIX system the easiest solution is to use expect, which is a library for simulating a terminal and thereby automating these kinds of interactions. It's often used as a standalone program embedded in Tcl, and I've thrown together systems in which Java launched expect to talk to tools like sudo, and it works fine.
expect includes a sort of declarative scripting language that tells it how to run another program and how to react to that program's output.
What you would do is use Runtime.exec() to execute the expect program, supplying a script that just runs "sudo mount", watches for the password prompt, and provides the password. The script would probably just look something like (G4rb4geB4rg3 is the password):
spawn sudo mount -o loop /home/user1/Desktop/aDirectory/system.img
expect "password:"
send "G4rb4geB4rg3\r"
expect eof
The problem was solved, by using shell script.
I wrote a script includes just this line :
echo myPassword | sudo -S mount -o loop system.img system
then I run it in my java code, such :
Runtime.getRuntime().exec("sh 1.sh");
I'm pretty sure 'sudo' and 'mount' would be separate, since it's not a single executable you're invoking. Also, if you start sudo with the -S command line switch it can take the password directly from stdin, so you just need to start the process and pass in whatever the password's configured as to the input stream.

Categories

Resources