I'm developing a java application. I need to call an user script in some point of my app, Since my app can be executed on windows, linux and osx I'd like to start a script in os independent way if possible. Currently I use this on windows for bat file:
Runtime.getRuntime().exec("cmd /c start user.bat");
But on Linux I should do:
Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", "user.sh" });
I can check the OS where the application is running, but I'm wondering if there is an alternative way.
Write the command in a config file, and read that file from your program. Make different config files for each operating system. When you distribute your program, include the config file for the target operating system.
Once a program requires things that depend on the host operating system, there are no good solutions.
Related
So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.
I want to execute shell script on remote windows server through java code without any server setup and installations like SSH. My script file is already there on server. I just want to execute it on server cmd from my machine. Any possible solution?
According to definition of shell script from wiki:
"A shell script is a computer program designed to be run by the Unix shell"
Taking into consideration that you want to run a shell script on a windows server it will simply not work. I would suggest you to use some kind of environment as it is provided by cygwin or any other alternatives.
You can also check the following question.
I am creating a batch file from Java which is deployed in a Linux machine and moved from Linux to Windows using Samba.
We want the batch file to be triggered from Java. Can you please highlight some steps?
I cannot use below command as it will use Linux run time:
Runtime.getRuntime().exec("cmd /c start buildFile.bat");
Any suggestions?
You can execute processes from Java on Linux as described here. The process is executed from the operationg system and depends on the operating system. You could rewrite your buildFile.bat as a linux shell script on execute it from you Java program.
I created Batch file in Windows using Samba from Linux machine and let windows scheduler handle running batch file.
Problem solved :)
I am running a server on my machine. When Servlet receives a message, the corresponding Visual C++ ".exe" need to start running.
I am using following code to start the exe. But I am getting "Microsoft Visual C++ Debug Error". The code is as follows:-
if(strLine.equals(location))//same place do not do anything
{
Runtime rt=Runtime.getRuntime();
String cmd[]={"cmd.exe", "/c", "C:\\Users\\nabeel.OUCS1289\\Documents\\Visual Studio 2010\\Projects\\Scene Localization - (FM)\\Debug\\Scene Localization.exe"};
rt.exec(cmd);
System.out.println("Same place so dont do anyuthing");
}
Please help me out in this regard.
The EXE file to be executed is located in a user profile directory. Does the account running the JRE/Webserver does have read & execute permissions on that particular directory?
Furthermore remove the indirect execution via cmd.exe /c .... This is total unnecessary for regular executables. It is only required in case you are executing a command that is provided by cmd.exe itself and therefore can not be executed via an exe file.
I am creating a desktop application. I know how to add program to system tray, that consists of a continuous system process, I need instructions on how to add java code to system configuration startup menu. Like antivirus program which automatically executes on starting the system. would be of great help with example code
Write a batch file(.bat) which executes you java program. Add this batch file into the registry in such a way that it will be executed during system startup.
simply write following into your batch file
java filename
In linux you will hv to create a .sh script(executable) that will execute your java program.
put .sh in /etc/rc0.d using following commands
cp name.sh /etc/rc0.d/
chmod +x /etc/rc0.d/name.sh