So I created a game launcher that successfully downloads a Zip and extracts it. But then it tries opening the Jar file that was in the zip, and all that happens is the game pops up for about 2 seconds then closes. If I manually double click it it works fine, or if I run it through the command line, it works fine... Here are the two snippets of code I've tried using to get the Jar running. (Take note the jar does not return any errors on run):
Runtime rt =Runtime.getRuntime();
rt.exec("java -jar \""+appdata+"\\gamefiles\\Game.jar\"");
and...
Process proc = new ProcessBuilder("java.exe", "-jar", appdata+"/gamefiles/Game.jar").start();
int result = proc.waitFor();
System.out.println(result);
Any feedback is appreciated, thanks.
Since you are using a process builder, and you don't specify anything about the environment, is there a chance that your JAR file can't find needed elements (like JVM location or classpath) because it's not getting an environmental variable?
Related
I'm currently programming a Python script that opens a file in an different directory that the user enters in an input box. Everything runs fine but the jar file I used for testing doesn't run or show up. I'm using the Shimeji-ee.jar in testing, and at one point its tray icon showed up but disappeared immediately.
I tried running it in command prompt(since calling the jar file using the script is similar to running a file in CMD) and discovered that it only runs as long as the CMD window is open. A few searches later, I've found a way to run files in CMD that keeps it running even after closing the CMD. I wrote it in my script, no errors, but the Shimeji nor its tray icon doesn't ever appear anymore.
I've added a line at the end of my script that is also told to be an efficient way of keeping the script running, but it doesn't work either(could be another mistake here):
while True:
keyboard.wait('q')
if keyboard.is_pressed:
sys.exit()
Here's the line of code in my script that does the calling:
subprocess.run(['D:', 'cd', PurePath(fileDirectory), 'START', '""', fileToExecute], cwd=os.getcwd(), shell=True)
The code I learnt that makes a file run in the background(similar to adding & in a Linux terminal):
START "" program
I've had thoughts that the jar file I'm using could be the problem, but I haven't found any answers for hours. Is there anything wrong with the code or am I missing something?
Update:
Code finally worked after the first answer but I received an error that seemed it read the file as a double forward slash like this:
# The network path was not found //
or
# The system could not find the file //
The solution I found was just removing the '""' part of the code, which makes the code look like this:
subprocess.run(['START', fileToExecute], cwd=PurePath(fileDirectory), shell=True)
The problem is that your code:
subprocess.run(['D:', 'cd', PurePath(fileDirectory), 'START', '""', fileToExecute], cwd=os.getcwd(), shell=True)
executes the command D: with the arguments cd somedirectory START "" fileToExecute, which changes the current drive of the shell to the D drive and then terminates.
You probably want to execute
subprocess.run(['START', '""', fileToExecute], cwd=PurePath(fileDirectory), shell=True)
so it was created from eclipse. Double click didn't work, a .bat file didn't work (it open cmd and close it immediately), i have updated java RE but nothing change. The only way to run it is to move it to C:\Users\user and open cmd and type java -jar xla.jar. I don't want to change any setting of the computer since i have to send it to my friend (this is a group project). Btw i'm on the new Fall Creator Window version.
try
java -cp xla.jar com.package.name.Classname
if you have main method on a specific class.. using a batch file and then run it .. if you cannot run it by double clicking just google and create a exe to run your batch file.
After searching the web about a problem I have I found that answer.
in brief, I need to spawn a new process from an Android application, and run a simple C program.
I've created simple C program, like the next one:
int main()
{
printf("This is the message before sleep() function\n");
while(1){
Sleep(1000);
}
printf("This is the message after 1 second");
return 0;
}
Iv'e complied the C program with Cygwin with the next command (gcc myProgram.c -o myProgram).
Iv'e put that file in the assest folder and I copied it at the begining of the program to the internal device memory to the folder "data/data/packageName/files/myProgram".
Now I want to execute the program, and when I will check, adb shell -> ps I want to see two process with the same name, but I can't find it.
I am trying to run the program like this:
Runtime.getRuntime().exec("chmod 755
data/data/packageName/files/myProgram");
This is not working, I can't find two process, and I don't know if this is the right way.
What I'm doing wrong?
Thanks.
You need to deploy the binary to the file system, to a location which is not marked as non-executable (that's why /sdcard/ and other common places don't help). You need to choose the version of your executable that matches the platform (armeabi, or x86, or mips). You should make sure that the file you deployed has the eXecutable permission.
The easiest way to ensure all this is to copy the arm build of your binary to ${project_root}/libs/armeabi/lib_myProgram_.so, and same for other relevant ABIs. Now the APK builder will pack the binary(s), and the installer will unpack them (with eXecutable permissions) to /data/data/your.package.name.with.dots/lib/lib_myProgram_.so.
All this done, you simply call from Java (the following line can be called from Activity or Service, which have access to context):
Runtime.getRuntime().exec(getContext().getApplicationInfo().nativeLibraryDir + "lib_myProgram_.so");
I have a Java standalone project in Eclipse with about 10 packages. I have a main method(in eclipse) that when executed from Eclipse works fine.
I have written a batch file to run it from the desktop. I just click the batch file and hope to run the program.
My code for the batch file is as follows.
RunExecuteMyProg.bat
echo Output of the Program
echo ---------------------
java C:\eclipse_workspace\eclipse\myprogram\MainProgram\ExecuteMainProgram
echo "Program Executed"
This program when run in Eclipse, usually takes between 1 -4 min depending on a number of factors. But when I click the .bat file, it opens for a fraction of a second and closes. Java is on my classpath. At command prompt when I try to compile, I get compile errors saying that some class is not found. However on eclipse it just runs fine. Log files need to get created when this program runs, but nothing happens from batch file.
PS: The class files are created in the same folder as the source files.
You will know the problem if you open a command prompt and enter that command you have there:
java C:\eclipse_workspace\eclipse\myprogram\MainProgram\ExecuteMainProgram
It could be that you don't have java in the path or your program is written so that it has to have its current working directory where the program is located, etc etc.
Or any number of things. Get the output from executing that command manually in a command prompt.
I have a java program, that has been exported to a runable .jar file. This jar run correctly in a windows cmd. I execute typing this:
java -jar completerute.jar
It runs correctly (execution lasts about 1 minute) and generates a file as result. All perfect.
But when I try to execute this in other project by typing this:
Process p = Runtime.getRuntime().exec("java -jar "+completerute.jar);
p.waitFor(); <--- this is because i need to wait for the .jar finish to read the file that it generates
The call is correct but it INSTANTLY says that has finished (p.waitFor() sets green light to continue the normal execution). But the resulting file of the .jar application hasnĀ“t been generated. I think that this is because the call has been done but the jar application hasn't been executed. I don't know why......
And I hope you could give me some orientation about the cause of the problem. Thank you for your attention.
EDIT: exact code from the source file is here
System.out.println("Calling jar process");
Process p = Runtime.getRuntime().exec("java "+System.getProperty("user.dir")+"\\esclavomaude.jar "+parametros);
p.waitFor();//waiting for finish
System.out.println("jar call finished");
Aclarations:
System.getProperty("user.dir")---> provides the actual path of the project which is on execution. my jar is on that directory.