Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
In my Spring-boot application, I am trying to trigger a shell script which in turns internally executes several other shell scripts.I am able to trigger my main script (say master.sh) using ProcessBuilder, but thereon any scripts being called within master.sh do not run. In fact, nothing happens.
At a high-level, I am trying to do the following in my master.sh -
Check for some condition
Execute another shell script This never executes hence no further statements execute.
make curl call back to my spring-boot app
.....
Any help please...
Quick recap of the use case I was struggling with - In my spring-boot app, user has an option to trigger a main script (say master.sh) - this main script internally does the below -
(a). Executes another script present at some other location on the system :: this script could take anywhere between 30 mins - 3+ hours to execute.
(b). Once step (a) completes, master.sh (main script) runs curl commands to make a callback to my Spring boot application in order to perform updates to the DB.
Above steps a & b are executed multiple times inside the master.sh
Solution : As for triggering any command from Java code, i used ProcessBuilder to achieve the same. But I had to add below additional statements before issuing processBuilder.start() call in order to make it work -
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Above statements redirect the script output to the parent process's (in this case Java) error and output streams.
At the moment it has worked for me. In case the behavior changes (which I don't anticipate) then I will update this thread accordingly.
Lastly, I would request others to share their understanding on why without the above two statements my internal scripts did not run. I do understand the "why" but not finding a clear wording to explain. Thanks All.
Related
I need to create a Java application which sends some input parameters to a python script and sends some output back to my java application.
I cannot run the script in my java code using jython Or similar things as the python scripts are build on demand and I may need to add new scripts every now and then. So this should not impact my java app.
My java application will be running on a container and based on a few condition check it might have to select 1 of the py scripts from suppose 100 scripts and run it. And again the condition later on may change and a different script has to run at that time
I went through many websites and tutorials on the net but did not find anything relevant.
Has someone tried anything similar?
I have a shell script which executes a java class and it is supposed to run weekly using Cronjob.
Last few days it hasn't been working as expected.
It does not generate any kind of logs or output.
checked /var/logs ,the cronjob looks like it is running on the time it has been scheduled to.
Also , if i manually run/execute the java class it works perfectly fine.
Any idea why cron isn't working?
PS: A similar shell script scheduled at a different time running the same java class is working without a problem.
Your question is if we have any ideas why cron isn't working. Based on what you wrote, I think that you probably recently moved the input or output file. It may run when you execute it manually but cron runs from a different location, so you have to use full paths to all files and commands, please try this. Source: Experienced same problem
Best Regards
This question already has answers here:
Run external program from Java, read output, allow interruption
(5 answers)
Closed 9 years ago.
I have been trying to work on this issue I had by searching thoroughly so as to find out what to do. However, none of the results I've found (at least until now) had suited my requests.
The fact is, I've got an executable JAR I've done. This jar starts an .EXE.
Now, the thing is, the EXE will keep on running the whole time, and I want to get whatever has been written in the console so as to write it on a JTextBox as soon as that is read.
Would you mind giving me an example of that? I would like to do it on my own, but my head doesn't seem to find out how.
Thank you very much.
EDIT: what I'm trying to do is a GUI for a gaming server
EDIT 2: for those saying its duplicate... wish it was... tried what the others explained but didn't work, so that's the reason I asked here..
EDIT 3: as I have been looking forward to find what the problem was, I will tell you that what I've done does not have any errors. However, I guess, it may be caused to the fact that the server (written in C++/C) may not output in a 'normal' way. May that be the reason? I hope so. Otherwise, I might be doing something really wrong.
Please notice I use InputStream in order to be able to read.. but well.
Basically, you need to start by running the process in some kind of background thread so there is no risk that it will block the Event Dispatching Thread.
Then you need to read the processes InputStream. As the input is read, you need to push these updates to the UI in such away so as not to violate the single thread rules of Swing. That is, you should ensure that all updates are made within the context of the Event Dispatching Thread.
Check out Concurrency in Swing for more details.
In this, I would recommend using something like SwingWorker. It allows you to monitor the process from a background thread, but has easy to use functionality to sync the updates back to the EDT.
Take a look at Printing a Java InputStream from a Process for an example
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have written some Java code to keep getting some value in other website every 10sec.
And I will keep writing the result to append a txt file.
How can I keep running this code.
Should I run it in some cloud server?
If so, how can I do it.
I just know in Eclipse, I click "run application".
How can I run in other ways(Linux, or without Eclipse)?
Here is what you can do, using a loop, shell script and a cron job to achieve what you need:
Make sure your program has the logic something like an infinite loop to run always.
Then write a shell script to invoke your java program with the necessary arguments.
Make sure that the classpath argument points to the jars that you need.
Make sure that the shell script has necessary linux permissions.
Schedule the script to be invoked by setting up a cron job. You can set the cron condition as per your need.
If you used eclipse, then you probably have created a jar file. In that case to run it outside eclipse:
java -jar <path-to-jar-file.jar>
Make sure you are in the correct folder before running above.
Eclipse is an IDE, meaning it allows you to edit and run code. When you click the Play button to run a project in eclipse, for each of your *.java files in the project workspace, another *.class file gets created. This is your code compiled to something the JVM (Java Virtual Machine) understands, and can be translated into machine languange and be ran.
These .class files can be run from the command line as well:
java myProgram.class
(Assuming you have the JRE in your environment variables). This can be done from any jvm on any platform, as long as your code is designed to be cross platform (Not using Windows specific APIs for example).
To stop the app, you can either implement a method that stops it, or kill its process. As for cloud computing, this is too broad a topic to just shoot you an answer here, but yes, you can run your code on a linux machine for example.
I have a workflow of functions that eventually lead to a deployment of something.
the steps are as followed:
run a python script the produces some output.
run a bash script that copies this output to a given location
run a bash script (depending on the output of 1,2) that runs a java program (running make take few hours)
run another bash script on this output - final step
i want to be able to initiate this workflow from a GUI giving a set of parameters.
Then, I want to be able to get status reports of each step - so that the running user will know which step is currently running.
The main issue here is that each of the steps may take long time to finish.
My experience with developing GUI in java is limited.
Any advice on the direction I should go to?
If you really want to write this in Java, then you should try to find a good wizard library. See this question for some details.
Then, if at all possible that your scripts provide some feedback like the percentage of the operation completed then you could have a JProgressBar for each step independently and an overall one to show the user an idea of how much is done at any moment.