I have a Java application that needs to implement installation features of making a JAR launch on startup.
I can do this on Window by entering a REG file into the registry, but how can I do this on UNIX platforms? Linux and Mac if the methods are different.
Do Linux and Mac have system startup folders?
Remember that I need to do this programmatically not through system preferences or anything like that.
On Linux, the classic way would be through adding a script in the appropriate /etc/rcN.d/ directory (where N is a number 0-6 representing the 'run level'). I'm not sure whether that's still the recommended way, but it usually is still supported. This would also work with minor variations for other mainstream Unix variants (Solaris, HP-UX, AIX).
On Mac, you have to work harder. The files /etc/rc.common, /etc/rc.imaging and /etc/rc.netboot are related, but there are no /etc/rcN.d directories. There's also a script rc and another rc.local. Typing man rc reveals:
DESCRIPTION
rc.local is now unsupported and has been replaced with launchd(8), which bootstraps itself via the launchctl(1) bootstrap subcommand to read in launchd(8) jobs from the standard locations.
SEE ALSO
launchd(8), launchctl(1)
So, you should investigate launchctl and launchd, particularly launchctl.
This is how I would do it on ubuntu.
First create a bash script to run the java app, similar to.
#!/bin/bash
java -jar "helloworld.jar"
and save it, in this case called 'HELLOWORLD' in /etc/init.d.
Need to make the script executable so need to run
chmod +x HELLOWORLD
Finally to make it run on start up
update-rc.d HELLOWORLD defaults
On Macs I think its launchd, and on linux its init.d. They are config files.
Related
I'm trying to run a simple program to test Java RMI but when I type rmiregistry 6000 into the command window I get the following message.
'rmiregistry' is not recognized as an internal or external command,
operable program or batch file.
I'm probably doing something stupid but I can't find RMI registry anywhere on the computer. Can someone tell me where to find it or how I can download it? I'm using windows 7 and java is up to date.
You are apparently using Windows. The (each) "standard" Oracle JRE installer for Windows puts everything in a subtree that you can override but defaults to **\Program Files\Java\jre<n> or Program Files (x86) for 32-bit java on 64-bit Windows, and copies a few EXEs java.exe javaw.exe javaws.exe and a few DLLs from the jre\bin subdirectory to \windows\system32 which is conventionally in your PATH because that's where lots and lots of other Windows programs are. The installer does NOT copy other programs like rmiregistry unpack200 keytool etc., so for those you need to either add the wherever\jre\bin directory to your PATH or explicitly specify the full pathname for the program. Or copy them to \windows\system32 yourself, but then you have to remember to remove or update them whenever your Java changes (which could occur automatically if you allow Java to auto-update).
The installer does also make some registry settings so you can uninstall from appwiz (like other wellbehaved Windows programs) and the Java control panel appears like other control panels and the auto-updater does its thing unless you disable it, and some settings (I'm not sure if these are registry or elsewhere) so browsers like IE and Firefox can automatically run Java applets.
I have a .sh scripts in Linux.
I want to create a form in Java (eclipse) on Windows
and execute those .sh files.
Is there any simple way to do this?
At first you need to be able to run those shell scripts on target computer.
So try Cygwin, MinGW or other Unix emulators.
If run successfully next step is to use Runtime.getRuntime().exec(command) for running script. You have to set up environment (run another command before) to make it working.
The gcc compiler on CDT Eclipse for Windows is working this way.
.sh is a a shell script, most likely the default shell for a linux system, i.e. bash, so no, it won't run on your Windows machine. You can try Cygwin shell but still there is no guarantee that it will run. You will have to rewrite it for Windows batch script or your some other scripting language.
As far as Java is concerned, you can write in Linux and the code is portable to any other machine supporting JavaVM.
No there isn't. Perhaps intalling Cygwin would be an option, but I would not recommend to go that way.
Or perhaps I misunderstood. If you like running those scripts on your linux-machine, you can trigger a remote execution easily by using the appropriate java commmands or libraries (eg: jssh or anything like that).
Have you had experience with running a jar file using a command line, wrapped in a Windows service?
I'm trying to find a way to run a jar file without being logged into the machine, and since it allows command shell, I was wondering if it's a good idea.
Thanks!
Original Post:
I'm trying to run Associated Press's Web Feeds Manager, which is basically a jar file that can be run when logged in by double clicking it.
I'd like to run the same file but without being logged in to the machine. In their manual (http://wfm.ap.org/admin/content/help/Running_Agent_on_a_Remote_Server.htm) they write how to do that, using a commandline parameter.
Basically I'd like the jar to run as a Windows service, regardless of who's logged in, but Googling it showed it was problematic.
Have you had experience with remotely running jar files? What are the pitfalls?
Thanks!
On a google search, I came across this article -
Running Jar Applications as a Windows Service
It mentions about open source Java Service Wrapper project from Tanukisoftware.org for accomplishing this task.
Note: I've not used this personally.
If you are not interested in having the service started/stopped at boot/shutdown, but you just want the program to be started manually and keep running after logout, here is what you do:
$ nohup java -jar foobar.jar > foobar.log 2>&1 &
which means: start my foobar.jar (java -jar) and keep it running after I logout (nohup) redirect stdout to foobar.log (>) and also the stderr (2>&1), and make it running in background (& at the end).
Instead, if you are interested in installing a "service" in your linux box, there are many options, depending on what distribution you are using.
The most common are upstart (for ubuntu) and System V init scripts (Redhat or others). Also cron can be used to start/stop services at startup/shutdown.
You can find an example of installing a java app (hudson) on an init system here, or doing the same thing with upstart. Or, as I said, cron could be an option.
On Windows, there is Java Service Wrapper. And not much more.
For windows Java Service Wrapper is a better choice
My favourite is the upstart on linux, but it is Ubuntu only.
On Windows I see many alternatives according to this forum.
I developed a project using Java and now I've to deliver it to client who is using Linux. Which executable file format will be delivered and how to make that?
Executable file format?
If you're delivering a Java app, give them a jar file (and associated libs).
Provide a shell script to set up its environment and execute it.
For example, assuming I define ROOT_DIR as my app's install directory, and so on:
CLASSPATH="${ADD_JARS}:${CLASSPATH}:${ROOT_DIR}/lib/myApp.jar:\
${ROOT_DIR}/lib/jibx/jibx-run.jar:\
${ROOT_DIR}/lib/jibx/xpp3.jar:\
${ROOT_DIR}/lib/bindings.jar:\
${ROOT_DIR}/lib/commons-lang-2.0.jar:\
${ROOT_DIR}/lib/forms-1.0.5.jar"
"${JAVACMD}" -Xmx256M -DanyDefsNeeded=foobar -Dbase.dir="${ROOT_DIR}" -cp "${CLASSPATH}" myApp.main.Launcher "$#"
What goes into the shell script depends totally on what your app actually needs to start up.
A jar. If it is not executable, then a script (.sh) to launch the jar.
Well basically what you wanna put in a .sh file is the commands you'd normally type at the console to run your jar file. They should be separated by a new line (i.e. each on a separate line in the .sh file).
The most basic you can go is add something like this to your sh file:
java -Xms=64m -Xmx=256m -jar myJar.jar -classpath [dependencies dir]/dep1.jar,[dependencies dir]/dep2.jar
beyond this you can do more exotic stuff, like parametrise some environment variables, get command line argumens from when the .sh is launched and pass them to the jar executatble etc. Look up "bash scripting" for advanced stuff:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-2.html#ss2.1
You might have better luck using Launch4J, IzPack or other installer that has cross-platform capabilities. This might be a better first option than trying to understand the intricacies and idiosyncrasies of the different Linux distributions and shells.
If your app. has a GUI, the best user experience for installation/deployment can be had by way of Java Web Start. Note that JWS can deploy apps. to Windows, *nix and Mac. and avoids all the maintenance woes of generating 3 separate (platform specific) executables.
I was looking for advice on how to execute a process that is somewhat cross platform.
I have written Java and Ruby implementations of my app, but its less of a language specific problem and more of a platform specific issue.
E.G on Ubuntu /usr/bin/ is searched for anything I do
# Ruby
`HandBrakeCLI #{args}` # works on Ubuntu and likely other linux distros since HandBrakeCLI is in /usr/bin/
For windows it will search the current directory I am running in. However, If copy the windows executable to the current directory a runtime linking error will cause it to crash (DLL not present)
The default install path for HandBrakeCLI is in "Program Files(x86)/HandBrake" (x86 only in 64 bit obviously), however Program Files isn't in the search path as far as I can tell.
For MacOSX I have no clue how to execute Mac "Application Folders" from the command line other than with the "open" command. But that is less of an issue since what I am really after is HandBrakeCLI which is just a normal executable. However HandBrakeCLI is not installed to /usr/bin or /usr/local/bin ( or any variation there of )
My best idea is to make a config file that has the path to HandBrakeCLI. If I go that route however it would be ideal to be able to detect what platform I am on, but that is another problem all together. One I have only done with C #ifdef sections and not in Ruby or Java.
I would appreciate any and all input.
You can get the current OS in Java using
System.getProperty("os.name")
In ruby:
require 'rbconfig'
puts Config::CONFIG['host_os']
Hope this helps :)