Failed to execute script in CMD - java

I have an .exe file on MyDocuments folder. I can't seem to run the program in the command prompt if I run C:\User\User\MyDocuments\Sample.exe. This gives me an error Failed to execute script.
But when I'll have the command prompt opened on the MyDocuments folder and only run Sample.exe, the programs runs perfectly.
What I want to do with this is I want to have a java program and execute Process p = Runtime.getRuntime().exec("C:\\User\\User\\MyDocuments\\Sample.exe"); and it will give me the Failed to execute script error.
Any I ideas what I missed?

As suggested by Application will not launch from command line full path, but will after CDing to directory I would make sure your executable does not depend on the current working directory.
Try Running:
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
before your try and execute your executable.

Related

"Error: Unable to access jarfile" command executed using Runtime exec

I am developing a Java application where one of its task is to execute a system command using Runtime exec. The command requires another JAR file to be able to work but whenever the part where the command will be executed, I am getting an "Error: Unable to access jarfile".
Firstly, my current working directory:
assets/jarFile.jar
myApp.jar
Here's what my code looks like:
try {
// To get the path of the directory where my current running JAR is
String jarPath = myApp.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String path = URLDecoder.decode(jarPath.substring(0, jarPath.lastIndexOf("/")), "UTF-8");
// The system command to execute
String command = "java -Xmx1024m -jar "+path+"/assets/jarFile.jar and-so-on...";
Process p = Runtime.getRuntime().exec(command);
...
}
The jarFile.jar is not an executable JAR by the way.
Any idea?
Seems like your code to find the path of the jar file does not return the correct path.

Execute command in a separate cmd or terminal

It is possible to execute external commands in java at run time.
However, what I am looking for is to actually open cmd or terminal and show that executing external command on screen.
I tried to execute a jar console application as java -jar jartool arguments, it works both on Windows and Ubuntu. The only problem is that, this does not open cmd or terminal separately. it runs the command in background.
We can use either ProcessBuilder and the start process or simply Runtime.getRuntime().exec.
I need to forcefully open the cmd or terminal. Java is configured to show console on windows but still no luck.
Any idea why this is happening or what should I do to force it to open cmd or terminal independent of current cmd or terminal.

Executing jar in applescript

I am writing an app that uses the .scpt file to execute a jar in the same directory. I know how to use applescript to get the current directory, and I know that I did it correctly because when I print how my command, which looks like this:
do shell script "/usr/bin/java -jar /Users/name/Desktop/test.app/Contents/Resources/Scripts/myJarFile.jar"
Now the problem is, if I copy and paste this into a terminal, it runs properly, but in applescript it gives me this error:
error "Exception in thread \"main\" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Runner.main(Runner.java:22)" number 1
Also, I have tried executing a shell script that runs the above command, but to no avail (when it is called from applescript of course).

Java: execute command in a new cmd.exe as administrator

I want to execute a command from a Java-application using
Runtime.getRuntime.exec(command);
but the command need Admin-privileges. If I use
runas /user:Administrator "cmdName parameters"
nothing happens because I need to give user und pw as parameter to the command.
But I need to run a command to the cmd, so that a new cmd.exe starts as administrator and asks if I want to run cmd.exe as admin. After agree the command should be run in the admin-cmd. So like this:
String command = "popupNewCmdAsAdminAndRun "batWhichNeedsAdmin.bat" "
Runtime.getRuntime.exec(command);
Has anyone an Idea?
Thanks in advance!
you should do the following
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "command";
process.StartInfo.Verb = "runas";
process.Start();
process.WaitForExit(60000);
Start Command Prompt (CMD) from java as Administrator and Run command prompt as Administrator - or the silly way, try to pipe it:
pw | command
Research building projects and the executable jar file. Usually when you build a project, it gives you a commandline that can be used to execute the jar file in folder dist located in the projects folder. You can copy and paste the command line in cmd. But you could also use a bat file, or convert the executable jar file into an exe with the jar2exe converter. Not completely sure of the name because i am not posting on my computer.

how to run shell script in java using Cygwin

From a long time i am struggling for with this program. I am having a shell script which accepts parameters as version number and path for the files. then that script creates Zip file with the name of version number congaing all file files to the folder.
I have installed Cygwin on following path D:/cygwin. I am coping required files to same location where cygwin is installed D:\cygwin\bin
Command
D:/cygwin/bin/bash -c '/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files
Or Can any one please suggest how to run shell script in java using Cygwin.
Rewriting the Problem:-
When i am trying to run following Command in command prompt it gives error
sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
Error:-C:\Documents and Settings\sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
/usr/bin/app.sh: line 51: lib/lib.sh: No such file or directory
But if i run the same command at
D:cygwin\bin\Test>sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
It works fine. Can any one suggest me how to avoid this kind of errors.
Runtime run = Runtime.getRuntime();
Process p = run.exec("D:/cygwin/bin/bash -c \'/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files");
p.waitFor();

Categories