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.
Related
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have created an java application and it is working in some machine but not in few machines. I am not sure if it is because of the jre.
How can I find the path from which my application uses the jre and how can I force that application to use a jre in a specific location.
Thanks in advance
To answer the question at hand, Java system properties hold the values you seek:
String jreLocation = System.getProperty("java.home");
String javaVersion = System.getProperty("java.version");
I add the second of these as your problem may be caused by differing versions of Java (if the error is, say, a java.lang.UnsupportedClassVersionError).
In terms of enforcing a particular JRE, it's not possible to force the JVM version from a running Java program (this would need to be in a shell script or similar). You could check the value of "java.home" and exit if it's not the one you want but this would be unwise as this will vary from system to system. A slightly better options would be to exit with an error message if the "java.version" is not within a set of supported values.
However the best approach would be to fix the original problem such that your program works on all the systems you need it to run on. To this end, I'd recommend starting a new question with the error you get when running the program on these other systems.
You can write a script file to run a specific JRE, however do note that the problem is most likely not the JRE's fault unless the major version is different.
You cannot force a JRE to be ran in your Java program, because it would be too late.
You can find out the path of the JRE being use by checking the value of java.home system property. For example:
System.out.printf( "java.home = %s\n", System.getProperty( "java.home" ) );
Changing the JRE depends, particularly, on how you run your application:
If you are running your program through a command-line interface, you would need to modify your PATH environment variable to point to the correct java executable.
If you are running your program through an IDE, you may need to define a JRE_HOME environment variable and restart your IDE, or change the running configuration of your project.
To find out more about other useful properties in Java, take a look at https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html.
If your Java application requires a specific JRE, I think your best bet would be to package your application as "self-contained" bundle, containing the JRE. As many have already pointed out, once your application has started, the JRE is already chosen, and the only option you have, is to check the system variables, and possibly quit the application with some error message.
You can read more about the process of creating a self-contained application bundle at Oracle's Deployment Guide: Self-Contained Application Packaging.
I wrote a simple java code that would take simple inputs from the user in the command window (of eclipse for me) using nextInt() and nextLine(). However, I realized that others need JRE (I believe?) on their computer to run the executable jar file made. So I was wondering if there is a way to get around that by making the app produce a window that is like the command window to have the same interaction as the command window in eclipse.
So, if I were to run the .jar or .exe then a simple window would pop up that acts like the console of eclipse displaying lines from System.out.println() and etc.
To run a java program you need the jre. There is no way around that.
If you need the console, nothing is stopping you from running the java program from the windows command line, which will do exactly what you ask for.
You still need the JRE.
Unfortunately, when starting to learn Java with Eclipse, many people miss the opportunity to at least start to understand how to do the same from the command line, which is, if you ask me, good to know.
For programs written in Java, they are compiled as a jar file, like you mentioned, and how these compiled versions of your source code differs from many other programming languages is that they do not contain the assembly/machine code like for example a compiled C program would have. They are instead compiled as bytecode. Which is special code for execution by a Java Virtual Machine. Here is a good Wikipedia reference: link
To answer your question, yes, others need a JRE (Java Runtime Environment) and this can be either:
Installed by themselves (this is what you mentioned)
Packaged together with your java app, to provide a download-and-click experience.
For option 1, assuming they already have it installed, they can simply run it by executing the jar file with javaw, more information on that is in this previously answered SO question
For option 2, the process is fairly lengthy and I'll point you to the official docs to refer to: self contained executables and Deploying java apps
If you have a more complex project with third party libraries and what not, look at this SO question
In the past, I've also found launch4j, a cross-platform wrapper to be very useful, it automates the process of going from jar to an executable (made a simple game that using Swing, simple and ugly thing it was), but the user still needs a JRE, nonetheless. :)
Hope this helps!
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 8 years ago.
Improve this question
I did a reminder application in java.I want to run that application only at 12 P.M So that it then generates a mail to client .The thing to generate mail is done,but the main issue now is how to make application run exactly at 12 P.M daily...
Use cron (Unix only)
Add this to your cron tab
0 0 * * * /path/to/your/file.sh >/dev/null 2>&1
file.sh
#!/bin/sh
java com.package.YourMainClass
edit
On Windows 8, take a look here : Using Task Scheduler in Windows 8
You can keep the application running and do the scheduling yourself. For example, using Quartz
If you're on UNIX, you can use cron.
It can be solved in two ways. 1. Have the OS run it once a day using cron or other scheduler.
The other is to install your program as serice using http://sourceforge.net/projects/yajsw/ or other service wrapper helper.
Then have your program run in the back ground, with a timer every minute. When its time, do the tasks. This is great as your program can easily accept config for other triggers as well. Advantage of installing it as a service is that it will run automatically on system start and keep running in the back ground. Make sure you catch exceptions, log them but try to keep running so the program does not exit on recoverable and transient errors.
In both cases you will make a non UI program with an optional UI program that writes to java preferences API to tell the daemon any user specific preferences.
The service app can be run from command line as well if you dont want to install a few users do not want to install it as a service.
For timer can see How to set a timer in java
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm pretty new to some of these concepts so I hope you guys take it easy on me over this.
I want to run a program at start up in Ubuntu which I called Message.java. I did a little research and figured I could move Message.Class (someone told me to compile .java into a class first) into /etc/init.d. Then I used these commands:
sudo mv Message.class /etc/init.d/
sudo chmod +x /etc/init.d/Message.class
sudo update-rc.d Message.class defaults
What I want is for a simple message dialog with "Hello World" to display at startup. So far, nothing happens. Can someone tell me what I'm doing wrong? Do I even have the right idea? (I'm just going off other posts). And if I'm completely wrong can someone point towards a good tutorial? Here's the code just in case:
public class Message {
public static void main(String[] args) {
// TODO Auto-generated method stub
Component frame = null;
//default title and icon
JOptionPane.showMessageDialog(frame,
"Hello World");
}
}
Firstly, init.d is run on system startup. System startup is not when you log into the desktop, but when the operating system has reached a certain point in the boot process. init.d is typically used for running background processes and services.
Also, when you put in init.d has to be executable. A .class file isn't executable. You have to run a .class file by using the java command (eg. java HelloWorld). You would need to make a bash script to start it if you wanted it to run at this point.
(These aren't criticisms, just things to try and understand about how the architecture works. I didn't understand them when I started either.)
However since you want it to run when the user logs in you'll need to use another method. Depending on what flavor of Ubuntu you have you'll need to do something different. If it's the default and a recent version then you'll need to figure out how Unity run things on user login. If it's a Gnome or KDE flavor you'll need to find out those respective desktop environments run things when a user logs in.
I'll assume you have Unity and see if I can find a good simple guide to set a 'program' (again you'll need to make a small file that run your java command) to run on user login.
EDIT:
This guide seems to be rather comprehensive on how to set a program to run on login. You'll want to put the full path to your bash script in the 'command' box.