Run program at startup Ubuntu [closed] - 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.

Related

Force an java application to use specific jre [closed]

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.

How do I make a java .exe to allow user to interact as I would in the eclipse command window?

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!

Generic error messages when compiling basic program through mac terminal [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I am using linux and emacs, I got a program working fine on one computer but when I try to compile it through my mac terminal it refuses to work, giving an error message that reads as follows:
Jamess-MacBook-Pro:uni_shit James$ javac /Users/James/Desktop/uni_shit/cp4a.java
/Users/James/Desktop/uni_shit/cp4a.java:43: error: class, interface, or enum expected
}
^
1 error
The code is copied and pasted from what was working perfectly. Here is my code:
http://s30.postimg.org/9zmp2rr35/Screen_Shot_2014_10_08_at_18_20_43.png
It looks like you've pasted everything from the terminal running Emacs into the Xcode editor, including the status bar Emacs prints at the bottom (which is not part of your code). Delete everything from -uuu to the end of the file.
In the future, save the file in Emacs (e.g. on a flash drive or online storage) and open it up in Xcode (or whatever editor you choose), rather than copying and pasting. That will cut down on errors like this; and besides, you might have a program longer than one screen some day.
I should add, in case you're interested, that Emacs works fine on Macs too. There's actually a version of it included in OS X, although it's pretty old; for the current version, there are a few options to choose from, of which the most popular seem to be Emacs For OS X and Aquamacs. The main difference is that Aquamacs acts a little more like a Mac app in its handling of keyboard shortcuts and things like that; but the standard one also includes nice Command key shortcuts for a lot of things.

How to keep running a program in Java [closed]

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.

Launch Application in a minimized state from Java

This is a followup question to one I previously asked:
start-program-if-not-already-running-in-java
I didn't get a great solution there (as there doesn't appear to be one), but I have a related question:
Is there anyway to launch an application in Java code (an .exe in Windows, not a Java app) and have it start minimized? Or perhaps to minimize it right after start? That would solve the focus issue from the other question and the already running problem would more or less deal with itself.
Clarification issues again: the Java client and the .exe are running in Windows and I really don't have the ability to write any wrappers or make use of JNI mojo or anything like that. I more or less need a pure Java solution.
Again, thanks for the help and I am more than willing to accept an answer that is simply: "This is just not possible."
Windows only:
public class StartWindowMinimized {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err
.println("Expected: one argument; the command to launch minimized");
}
String cmd = "cmd.exe /C START /MIN ";
Runtime.getRuntime().exec(cmd + args[0]);
}
}
Sample usage:
java -cp . StartWindowMinimized notepad.exe
java -cp . StartWindowMinimized cmd.exe
To understand the arguments involved:
cmd /?
START /?
I'm not that familiar with the specifics of Java, but according to a web site I just looked at, if you're using java.awt.Frame (which includes JFrame from Swing), you should use the function off of that frame called setState, which accepts Frame.ICONIFIED and Frame.NORMAL as a parameter (iconified would be the minimized state).
How do I minimize a Java application window?
If these apps have command-line switches to make them start minimized, then you can easily use those. Otherwise, I can't be 100% sure, but I highly doubt this is possible. You would have to have some way to interface with the Windows window manager, which is inherently very platform-specific and Java is therefore unlikely to include it. It's always possible that someone has written a third-party library to handle the task but it just doesn't seem likely to me.

Categories