running .jar on eclipse - java

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.

Related

Java Jar files closing after running for 3 seconds

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?

.jar File Not Working With Windows Scheduler

Hi all so i have wrote a program in java (using eclipse) and exported single class program to a .jar file. This program also starts a batch file. When i double-click the .jar file the jar runs perfectly and starts the batch file.
But what i want to do is for the .jar file to run every weekly, so with windows scheduler i created a task with the action being the .jar file. This did not work. I then read somewhere that windows scheduler dose not like .jar so i thought of making a second batch file(start.bat) to start the .jar which would then start the first batch file.
The command in my start.bat is
java -jar myJar.jar
When i double click the start.bat file everything works. But when i set the windows scheduler to start this task i get the following error message for a cmd window
Error: Unable to access jarfile myJar.jar
This really has me stumped as all the files are in the same directory.
Any help would be seriously be appreciated, thanks.
Obviously this comment was the answer:
use the full path of myJar.jar instead of a relative path - the running directory of the windows scheduler is C:\Windows\System32 and your jar-file is probably not in this directory.
Task Scheduler cannot run .jar directly you need to run it via command prompt.
Because the task scheduler runs the .bat via cmd its default executing location i.e, C:/windows/system32 we need to change the path.
While scheduling task in scheduler call TaskName.bat as action.
SO,
Create a batch file "TaskName.bat" In TaskName.bat
Enter the Following
#echo off
cd "Path to the jar file Example C:\MyFolder"
java -jar Nameofthejar.jar
pause
If you follow the following steps you will not get any issues.
Step 0 : Setup
Add app.schedule.externally_managed=true in application.properties
Step 1: Create a New Task
Click Create
Provide details
Configure for Windows 10 is important
Step 2: Trigger Details
Step 3: Action Details
Step 4: Actions
Make sure all the checkboxes are unchecked as shown below, this is important
Step 5: View Task Details
Refer this and this for more details

Using task scheduler for running java jar (from batch file)

I am scheduling a task (with windows task scheduler) which simply run a batch file.
this batch is running a jar file (Java program) with a simple "Java -jar test.jar".
When i run the script from the command line manually, the java program runs well and no exception is shown.
but when the task schedular does the same (calling the command), the java program ends with an exception: "ClassNotFoundException" for one of the classes which is in my jar.
What way be the cause of this? what is the diffrence when calling the jar from the command line and from the task scheduler?
Thanks.
I reckon that probably the "current directory" is different, and as a consequence java doesn't find the jar at all. In the first line of your .bat, can you do a cd \path\that\you\expect before you execute java?
Does your jar have any dependencies? Also, it'd be helpful to know what's your folder structure, and how exactly do you run it in the command line.
Anyways, depending on your case, you can do something along these lines:
cd /path/to/exec/folder // set current directory
java -cp /all-classpath-jars/and-or-bin-folders/ test.jar your.package.MainClass [args...]
This has to work, if you specify everything you need correctly.

Error Running batch file from Windows desktop to execute a Java program in eclipse workspace

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.

How can I start and stop a jar file using another jar file?

I want to start and stop a jar file using another jar file or java class.
I am able to start the jar file using this command:
Runtime run=Runtime.getRuntime();
Process process=run.exec("java -jar setvalue.jar");
The process is started and working fine.
But I am not getting how I can close that process from other java class / jar file.
I want to run setvalue.jar file as a background process.And able to start and stop that using other java file and jar file.
Can some one have an idea how can I do that?
One option will be in your setvalue.jar Program create a thread running in while(true) loop which keeps looking to a file in certain place and whenever this thread find that file it will call System.exit to kill current JVM. When you want to exit that program just create that file with another java program of use "touch" to create in linux.
You can stop the process from the class which created it:
Runtime run=Runtime.getRuntime();
Process process=run.exec("java -jar setvalue.jar");
// Do stuff
process.destroy();
I don't think it is easy (or recommended) to attempt to stop an independent process using Java. However, if you are using a UNIX based system you can use the Runtime object to exec the ps command to find your process and then kill it.
If you're just looking to start your second jar file in the background you can use:
Process process=run.exec("nohup java -jar setvalue.jar &")

Categories

Resources