I would like to write a program that sends an automated email based on a timer that runs constantly. I would then like to somehow export this program from eclipse to a computer that does not run the ide, and run it constantly in the background. I have figured out the code to send emails through java, my question is more regarding how to export this project as an application (or something) that can be run on any computer without running it through the eclipse IDE.
Any help, or directions to a better a resource to learn from, would be greatly appreciated.
The simple (manual) approach to turning a Java program into something that runs outside Eclipse:
Create a runnable JAR following the instructions here: http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm.
If your application depends on library methods that are not part of the Java SE library, pay particular attention to the "select library handling strategy" step.
Run the application from the command line as follows:
$ java -jar yourapp.jar arguments ....
Obviously, you need at least a Java JRE installation on the execution platform to run java, and you should have configured your system (the $PATH or %PATH% environment variable) so that typing java runs the correct thing.
If you are using a build system like Maven, Ant, Gradle and so on, you can automate the step that generates the JAR. (In fact, you can automate the entire build ... and break your dependency on any IDE.)
I DO NOT recommend trying to create an "executable" for your Java application. For a start, executables cannot be run on any computer. They can only be run on computers running a specific operating system / OS family. A second problem is that you are effectively embedding a JRE in your application. That makes applying the latest Java security patches difficult.
As for the problems of keeping the application running "constantly" and sending emails at specific times, that is just Java programming.
Use Timer & TimerTask - e.g. http://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/
Use a job scheduler. For instance Quartz has an easy to use API for running jobs on a fixed schedule: http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06
You need to create a runnable executable. You can do this by following these steps: http://www.wikihow.com/Create-an-Executable-File-from-Eclipse
Regarding the timer/scheduler, you may consider using Windows Task Scheduler (on Windows platform) or cron (*nix platform).
You will probably need to provide more information about the requirements you have for the timer in order to get a more specific answer there.
Related
All of the solutions I found on stackoverflow suggest wrappers to register java application as windows service. My requirement is totally different. Please don't suggest wrappers for the purpose. The question is very simple I have java executable and I want to register it as windows service.
Phyiscal Path
Service Properties
Unfortunately we don't have backup of previous setup that installed it as windows service at the first place. Do I need some setup program or anything like that.
Not necessarily.
It is difficult to advise you on precisely what you need to do without more information on what you actually still have; e.g. an application installer, application JAR files, wrapper scripts, etc. Alternatively, if you told us what the application was, then maybe we could give you some hints on where to get installers, etc.
However, I can tell you definitely that registering java.exe or javaw.exe directly as a Windows Service will not work. These are not the executables for your Java application. Rather they executables for as Java Virtual Machine that will run your (real) Java application.
It is so easy task in case of Visual Studio. I want same support in Eclipse or anything else.
Well Java doesn't work like that. Java compiles to platform independent bytecode files, not to platform-specific native code. Sure, there are third party tools to generate exe's. However, using them is neither necessary, or desirable:
Why is creating an .exe from a java program not recommended?
(And asking for recommendations on what tools to use to do this is off-topic.)
Finally, if you take an arbitrary Java program and turn it into an ".exe" file, it won't necessarily be immediately registerable as a Windows service. This Q&A talks about turning an ".exe" into a Windows Service.
Create Windows service from executable
However, I can't tell you if the advice given there is appropriate for an ".exe" file created from an arbitrary Java app by some unspecified 3rd-part tool.
My recommendation:
If you are starting from scratch, use a Java Service Launcher / Wrapper.
If not, talk with whoever supplied and/or installed this application in the first place.
If you can't find any information about the application and where it came from, or if the vendor has gone out of business ... you need to urgently look for an alternative.
I have a large Java project that uses some Ruby scripts (primarily because of Ruby's support for "yield"). The Ruby code calls Java code which calls more Ruby code. It's very interleaved, but everything is driven from Java.
I'm using embedded jruby-standalone and building a jar-with-dependencies (via maven). I'm using a maven plugin to run jrubyc and generate .java files which maven compiles for me.
When I run the jar-with-dependencies, I can attach my debugger to the Java process with no problems, but I'd really love to be able to debug the Ruby code. Is there a solution for this?
I'm not launching any kind of jruby executable to which I could attach arguments. It's embedded in the jar and invoked via java -jar.
You could use the gem pry-remote.
Unlike pry, it does not require the process to be launched from a terminal (or a terminal emulator if you're on Windows).
It's not really a debugger per se, but if you add binding.remote_pry in your code where you wish to observe and react within that context (you could for example catch an exception), this would put pry in waiting mode for a remote connection, and from another terminal you should be able to connect to this process and debug it.
2 minutes hands-on tutorial is available here.
Drawbacks:
you cannot have 2 pry remote sessions.
your code must contain the right 'debugging' condition
I use this in pre-deployment environments when developing web apps with jruby, h2 and jetty server.
Good luck!
Is there any way to avoid the delay caused by the JVM starting up each time you run an exec command in PHP?
I have two JARs for encryption and decryption that I need to run using PHP. Both run on the same script although one decrypts a URL parameter and then the other encrypts some other information. When I run them through the command line, they both finish in less than 0.4 seconds each. However, when I run them using the PHP exec function, a new instance of the JVM is started which adds 5 seconds onto each JAR execution time.
I have investigated using Nailgun but can't get that to work. I can't find any documentation for getting it to run a JAR and when I use classes, it can never find them either.
I have also considered using PHP/Java Bridge. I would prefer however to continue using exec. I am already running IIS 7.5 and I am not sure how one would configure the bridge to work with this.
My question is this: Is there any way to keep the JVM running in the background in such a way that the PHP exec function does not need to start a new instance each time? I think there must be a way as there is no delay through the command line.
If there is no way of doing this, then I am open to other suggestions. 11 seconds to run a PHP script means that visitors to the website will most likely leave.
Additional information that may or may not be of use:
It will be running on Windows Server 2008 R2 32-bit OS.
Local access required only.
IIS server 7.5 being used.
Website is coded in PHP. PHP version is 5.3.5.
Server is running the latest JRE - Java7 u6
Is there any way to keep the JVM running in the background in such a way that the PHP exec function does not need to start a new instance each time?
You can start a ServerSocket on a known port.
If its the first time the application has run, this will be successful and this process can keep running.
If this is not successful, the application can open a Socket on that port and send the command as required and get the response.
Even though the accepted answer mostly resolved my problem, the JAR execution time was still taking slightly too long. I realize that the question deals specifically with the JVM startup time but if anyone else has a similar issue, this may be of help to them also.
I had been generating executable JARs using Eclipse. I was packing the 3rd party JARs inside my JARs. On examination of the generated JARs, I noticed Eclipse had added a lot of it's own classes one of which was in the manifest as the main class (it dealt with loading JARs within JARs I believe). I also noticed that all the files in the JAR were compressed (as you would expect in an archive).
Seeing as my JARs stay on the server machine and are never transferred over the network, I seen the decompression as an additional overhead that could be easily avoided.
I decided to make three simple changes to my JARs based on the info above:
1) Generate the JARs using java command in the command line and write my own manifest. This eliminated the additional class files that were being added by Eclipse.
2) While generating the JARs through the command line, I added the 0 option to disable compression.
3) I did not include the 3rd party JARs in my JAR archives. Instead, I included them in the same folder and added them to the classpath option in each generated JARs manifest file.
This reduced the time it took to run the JARs by ~269%.
We've developed a small app using play!, and it runs standalone on a user's computer (Windows XP or Windows 7), as the users don't have network access. Currently, it runs within a console/command window. I would like to make it so there's no console window in the taskbar where play is running--really, mostly because the users are prone to closing the the window. I came up with a couple of ideas, but I was hoping for some validation or other ideas.
First, I was thinking of running Java via javaw, which is typically what runs for GUI applications running Java--but what would I run? I guess I could run the Winstone Servlet container with the WAR output of play! (mentioned as a solution to the other stackoverflow question below).
Second, I was thinking of trying to wrap it in a windows service.
This question is similar, but slightly different (I don't mind installing play!, which just involves unzipping the framework):
Deploy Play! application as executable jar
Has anyone used either of these techniques, or is there a better way? Pros/Cons/Examples?
Thanks!
Update: Any comments about Winstone? It turns out that due to security constraints, we'll not be able to [easily] create services or scheduled tasks (as SYSTEM). Thanks again.
I have done something similar for a few applications. The steps I carried out are
Create a bat file that launches the play app. This could be as simple as play run or play start
Create a scheduled task that executes on system startup, using SYSTEM as the user profile to run the command as.
This approach has been working on my internal systems for well over a year.
Alternatively you can use yajsw to wrap your play application as a windows service. It works really well and is easy to set up.
The only issue I have encountered is that on one of my box running on Windows Server 8 R2, some of the jobs are duplicated when the scheduler kicks off. I am not convinced it is directly related to yajws and I have not been able to reproduce the error on other environments with the same setup.
I managed to work around it by adding a synchronization block in my doJob method.
Have a look here for info for setting up yajws with play.
I am not sure if play would work on winstone. A list of application servers known to work with play can be found here.
You will need to war your play application with play war myapp -o myapp.war and test it first.
I ended up creating a batch file that runs "play run", and a runnable JAR that calls "start /B runPlay.bat" as a system call. This way, the console window doesn't start up at all. It works great so far. There seem to be a variety of solutions, but this was fairly simple.
I have a Pure Java Process which needs to be run every day at certain time.
This is a temporary job, so i don't wanted to create a new environment to configure this job.
strong text
Is there any way to configure this batch at certain time in Eclipse or IBM RAD?
Is there any plug in available for that.strong text
Regards,
Lokesh.
To my knowledge there is no standard facilities available in Eclipse to support "run code X at time Y", so you need to turn to operating system facilities.
If you can make a simple program launchable with "java -jar" you have many options. FOr Windows many different EXE-that-launch-jar products exist (we use winsw to launch java as services) and you can then use normal scheduling (e.g. at) to invoke your code.