Java command-line launchers - java

I am working on a .jar file library to implement a bunch of helper classes to interface a PC to a piece of external hardware. I'll also add some simple applications, either command-line or GUI, to handle common tasks using the library.
My question is, is there a recommended way to simplify the command-line instantiation of a JVM in a certain specific way?
e.g. instead of requiring a user to type a cryptic error-prone command like:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar arg1 arg2
instead I do
TurboBlenderApp arg1 arg2
?
I suppose I could use shell scripts (incl. win32 Batch Files... gacckkk), it's just that I'm not good at those + I was wondering if there was a more straightforward platform-independent way of running a Java app from the commandline.

When you use -jar, then-cp (and the CLASSPATH variable) will be ignored
Just provide a executable jar. "java -jar TheApp <whateverargumentsyouwant>" shouldn't be too hard (you can have a Class-Path attribute in your jar files manifest, however).
if it is too hard write a GUI
or provide those shell scripts/batch files. Writing those isn't too hard either.

If you just want to simplify local execution, use a batch file or even just create a custom shortcut. If you want to create a launcher and package the executable jar and required libs for deployment, use something like Launch4j.

For some reason this trick just doesn't get around. What you need to do is to create a custom manifest for your jar file that defines the Main-class: property, and include in the jar file all the class files you're using. Then all you need is to run
$ java -jar myapp.jar
On real operating systems, you can even just run the jar file, as they will use the magic number to start it. But if you must, a one-liner batch or shell-script containing that line is all that's needed.
This is described in one of the Java Tutorials.

Shell scripts and batch files are the standard way of accomplishing what you want. (Look at any major Java product.)
This is, of course, absolutely pathetic and ridiculous. Go Java.
Your alternative is to write a little C program that does what you need (creates the java process) and compile that for each of your supported platforms. (C is the truly platform independent language).
Either way, you will need to take platform-dependent steps to make your app feel truly at home in the OS. On OS X, you will need to make a .app bundle, on Windows you need to package atleast the icon and version info into an EXE. For Linux, well, shell scripts suffice. :-)

If you use java -jar to launch your application, you can add additional jars to the classpath by adding a Class-Path entry to the main jar's manifest. See the jar file spec for more information.

Another solution is found in the Jakarta Commons, as always : commons-launcher.

A batch file would do the job. Put the exact command you want executed into a file myProg.bat and run it.

It's pretty easy:
Write this in TurboBlenderApp.cmd:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar %1 %2
// Bear in mind saua answer about -cp and -jar
Then from the command line you just write:
TurboBlenderApp arg1 arg2
Just the way you wanted.
If in Unix/Linux replace th4e %1 %2 with $1 $2 and make sure your app has execute rights ( If you don't know how to do this, I guess you don't need it either )

Related

What is batch file and what is the real time use of batch file?

I am new to java, Now i am learning. So i googled sample java websites. for example i downloaded source code for online reservation system.
In that code,it have install.bat and start.bat file. May i know what is the use of this file?
Can anyone explain me? Thanks in advance.
You can find out what theese files do by taking a look inside. .bat files are written in plain text. Each line contain one or several commands that can be interpreted and executed by cmd.exe. This kind of files aren't only used for java but for lots of purposes. In your case they are used to launch a java program. In contrast to e.g. C++ Java won't give you an .exe file that you can start by doubleclicking on it. It compiles your code into a .jar file. In order to launch this file you must call the Java virtual machine and tell it to start your jar file (there are some more parameters which you don't have to care about while you are just starting with Java).
However, it will take a while till you'll have to deal with BATCH files. By now you should use your IDE (I would recomend Eclipse) which allows you to start your progrgam without taking care of how to do it. You just hit the "Play"-Button and get your result.
They're nothing to do with Java per se.
They're Windows batch scripts, and from the names, it looks as though one of them is there to install the application on your machine, and one is there to start the application up.
Java is cross-platform, which is a strength for the most part, but it does mean that sometimes applications need a little bit of boilerplate code for starting the application on different platforms. So sometimes you will see Java applications that have a Windows batch file or similar for starting on Windows, and a shell script for starting on Linux, and so on.
Batch files with the extension .bat have nothing to do with Java. They are Windows-specific shell scripts, introduced with MS-DOS in 1981.
A set of multiple commands called as a batch file. Say you are been assigned to compile a set of classes, create jar file and execute. This can be done with a simple batch file like this:
activity1.bat
REM set java home path
set JAVA_HOME=C:\Program Files\java\jdk1.7.0
REM set path
set PATH=C:\Program Files\java\jdk1.7.0\bin
REM set class path
SET CLASSPATH=..
REM compile class files
javac -cp %CLASSPATH% %CUR_DIR%*.java
REM create a jar file
jar cfm new.jar Manifest.txt *.*
REM execute
java -cp %CLASSPATH%;new.jar MainClass.java
PAUSE
Note: REM meant for comments.

How do I create a Unix load card for a java program

In Unix (or Linux), if I want to run a shell script, I can start the file with #!/bin/sh. With awk, I start the executable file with #!/usr/bin/awk -f and it treats the rest of the file as the program.
How do I do that with a Java program? I tried copying the simple.class to simple, putting #!/export/appl/Mail/java/bin/java at the top and making the file executable, but I get:
69> ./simple
Error: Could not find or load main class ..simple
I know this can be done with an executable shell script, or a C program that execs the java interpreter. Every other interpreter on Unix can be called with a #! load card, surely there's a way to do it with Java.
The most usual way is to have a wrapper for the Java. A shell script that executes the "java -jar yourJar.jar" or equivalent. And then you bundle the shell script and the windows equivalent bat file with your product.
Another option is to have a native launcher. For example you can see the Eclipse project which has gone that way. You download Eclipse and you have a native executable to run. The native executable will launch your Java program.
One more option is to compile Java into native code. For example you can use this commercial tool called Excelsior JET ( http://www.excelsior-usa.com/jet.html ).
The Java class file format doesn't allow text before the header, that's why the Java runtime no longer accepts the .class file after your modification.
On Linux you can use binfmt_misc to support additional executable formats, including .class files. It's basically a way to tell the Linux kernel how to detect executable formats and how to execute them.
This Archilinux Wiki article explains in more detail how to get this effect.
You cannot do it with a Java program. Firstly, the Java program needs to be compiled before execution. Secondly, even if compilation wasn't required, the hash sign is not a comment in Java, so that would be a syntax error.
I've never heard the term "load card". What you have is an "interpreter directive" designated by a shebang. This merely designates which interpreter the shell should invoke on a given script.
As for why C programs can be run directly in the shell, executables recognized by the operating system are passed to the loader. A Java class isn't an executable, at least to the OS anyway. So the shell must know which interpreter to pass control to.
But as you've noticed, the shebang doesn't work. The reason is that the class file is in a specific binary format that the JVM expects. Editing this file will break convention and lead to an error. Therefore, there is no way to do what you've asked.
However, you can create a "shortcut" to the program you want to run by creating an alias or perhaps writing a one-line Shell script to wrap the java command you need. This is the common practice as I understand it.
The other answers explain why you can't do what you are trying to do. However, if your shell is zsh, you can create a suffix alias. For example, to execute .jar files using java:
alias -s .jar="/usr/bin/java -jar"
Then, to execute blarg.jar, you just type ./blarg.jar. Of course, you must chmod +x your .jar file first.
Apart from the wrapper script and binfmt_misc solutions suggested by others, I'd like to suggest a potential solution which doesn't directly answer your question but maybe it solves your actual problem.
Since Scala does have an interpreter that can run code without you having to compile it first, and this code can reference any Java code, if your goal can be summed up as "using Java as a shell scripting language", you could use a .scala file as your starter script (which can include the shebang to be run with scala) from which you can call all your Java classes. This isn't any simpler tha having a bash-based starter script, but it's a good starting point to gradually move to scripting in Scala instead of Java in which case you can get rid of the need to compile to .class file in the first place.
The reason this doesn't work is that Java isn't really an interpreted language, it's partially compiled, partially interpreted.
The .java source code that you'd put the hashbang directive in needs to be compiled to a .class file before the java interpreter can run it. Comments are stripped out by the compiler, so there's no way to push a comment from the .java into the .class file. The .class file is "compiled" output in a specific format, so adding a hashbang directive to the top of it would break the format.
Java isn't really meant to be a scripting language - but some JVM languages are. For example Groovy supports hashbang and so does Clojure.

Running JARs without java -jar unix & windows?

I want to run a jar file in both unix and windows without have to call it directly with java like:
java -jar myjar.jar parameters
i want :
myjar.jar parameters
I've been reading allready -
Running a JAR file without directly calling `java`
Which seems like a very nice hack for unix .
Howerver , this wont work in windows.
I'm looking for a uniform solution that will work both on unix and windows , but I'm not sure there is such.
The solution has to be only once , and it has to include changes related to the jar only ,and not the operation systems - because this is a file to I'm suppling to a client.
What you are asking can't be done: Windows will load executable files only in the PE/COFF format used in .exe in .dll files.
What you can do instead is supply the users a "wrapper" program that starts the actual Java program. You could create the wrapper in C, which has several benefits: you can set an icon on the executable and associate the program with file types in the Windows Explorer. Batch files are a popular alternative; they are easier to create.
You can provide a script that starts your application for both Unix (.sh) and Windows (.bat). This seems to be the preferred approach for many companies. An example would be JBoss Server where a run.bat is provided for Windows and a run.sh is for Unix. These scripts set the appropriate environment variables, classpath, etc and then call java.
You can write your own bash and batch/powershell scripts. As for windows, you can try Launch4J. It should be easier than writing elaborate scripts from scratch.
Be aware that you can only provide wrappers to make the execution simpler (one click). Java has to be run anyway. Either explicitly, by the user, or as part of a script. You can't do without it.

Cuda-memcheck and JOCL, can a java executable make use of it? (OpenCL)

I love JOCL, Java bindings for OpenCL. I would like to run Cuda-memcheck on an executable from Java, but whenever I make Java applications, they are always just JAR files that point to a Main-Class. Is there a way to create a .exe file like C++ does and feed that to Cuda-memcheck?
This link could be useful: http://jsmooth.sourceforge.net/index.php
You could probably also the jvm executable directly and provide all the arguments necessary to run your java application. If you normally run your java applications through an IDE, check the console output to see what command the IDE uses to launch your app. This command should resemble something like: <path-to-java-exe> [JVM arguments] main_class [application arguments].
You might try looking into one of the Windows ports of the gcc toolchain. I know that gcc has the capability to turn java code into compiled binary, which is what Cuda-memcheck is looking for. If you aren't afraid of a lot of unnecessary output, attaching Cuda-memcheck to the call to the JVM should also work.

Problems compiling file on School's Unix System

COMPLETE EDIT BUT SIMILAR PROBLEM
What's the best software/plugin to enable FTP on Eclipse? I'm using FileZilla, but is there something better/easier?
You are telling javac to compile gamedata.txt and it is reporting an error that it cannot compile this file.
I'd highly suggest using a tool like Ant to script your compilation/packaging/etc so you don't have to worry about typing in arguments on the command line.
First of all, the -J command line argument is not meant to be literally passed as -J<flag>. Taken directly from the javac man page (you can view the exact same thing by typing man javac into the shell):
-Joption
Pass option to the java launcher called by javac. For example,
-J-Xms48m sets the startup memory to 48 megabytes. Although it
does not begin with -X, it is not a `standard option' of javac.
It is a common convention for -J to pass options to the underly-
ing VM executing applications written in Java.
Really, if you want to make this an executable, you can just use the tools that exist in Eclipse to make an executable. Using the command-line javac adds an extra level of complexity that is unnecessary, and that Eclipse is specifically designed to remove.
In eclipse, you can (I think) use File->Export->Java->Executable JAR File to make your project into an executable JAR that any computer with the Java Virtual Machine can run. That way, your project will work on both your computer and the Unix system at your school. You may have to add gameData.txt manually to the JAR or include it separately in the package, not sure how Eclipse does that type of thing though.
You can only compile .java files. If you remove the .txt file from the list of files to compile, it should work fine. If you want to compile all the files in a directory, you can simply use javac *.java
There are some examples in the javac synopsis.
Edit: Updated link to Solaris examples, which are similar to Linux.

Categories