Is Java required to run some .NET based programs? - java

Introduction
One of the comments to this question is about using GnuWin's file command in order to check whether a program is using java or not. However GnuWin's file command indicates the following:
C:\Windows\system32>file "C:\Program Files\Eclipse Foundation\eclipse\eclipse.exe"
C:\Program Files\Eclipse Foundation\eclipse\eclipse.exe; PE32+ executable for MS
Windows (GUI) Mono/.Net assembly
C:\Windows\system32>
while java is required in order to run programs, e.g. Eclipse, ApacheDS, Apache Directory Studio and Tomcat.
The discussion regarding this question resulted in a suggestion to ask a question at StackOverflow regarding the relation between .NET based programs and Java processes.
Question
Why does GnuWin's File Command indicate that certain programs are .Net based, while these require Java in order to run?

Eclipse.exe is not a java program: it is actually a native win32 executable that serves simply to locate and launch the JVM with appropriate commandline parameters and the path to the JAR file that contains the actual Eclipse java executable.
See for example http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Flauncher.html for full details.

Related

Start separate process to run Java program using same JRE

I want to start a separate process from my java program to run another java program using same JRE that the current java program is executing in. Normally, I could get the path to the java executable using System.getProperty, but the java program is running in a bundled jre (Mac app package) which doesn't actually contain a java executable. Therefore, I'm wondering if there is there any API to directly run a Java program in a separate process?
Javapackager from Java version 9 on includes the bundler argument -strip-native-commands which leaves the executables in the bundled JRE. Just include the option:
-Bstrip-native-commands=false
The API is public hosted here: http://docs.oracle.com/javase/8/docs/api/
and the information you are looking for cons from the System utility class:
All available properties are listed here: http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getProperties--
The current JVMs location is available via "java.home".
So what your looking for is:
String javaPath = new File( System.getProperty("java.home"),"bin/java").absolutePath();
This may give a better picture.
Get the Java executable using below.
System.getProperty("java.home") + "/bin/java"
ReConstruct the class path,
((URLClassLoader() Thread.currentThread().getContextClassLoader()).getURL()
From here, you can start the new process using
Process.exec(javaExecutable, "-classpath", urls.join(":"), CLASS_WIH_MAIN)

run jar on mac with arguments

I have a bioinformatics related tool can be found here. The tool can be download from the link under that page.
It is a Java packaged files for which on Windows I can run
a compiled version (see the readme, need to change name of Mold2.doc to Mold2.exe) using the following command line:
Mold2 -i TestCompounds.sdf
(I have moved the TestCompounds.sdf file to same path as that exe)
It will produce some message and output 2 files output.txt and report.txt.
The trouble is that this package doesn't have any pre-compiled version executable for Mac OS.
However, since Java is supposedly cross-platform and since I also have the java Mold2.jar file, I think it would work for me if I can execute under Mac OS. But I can find the way how to do that...
I have tried with:
java -cp Mold2.jar Mold2
But it only invoke the GUI, and I can't find how to execute this program with only arguments under terminal without invoking the GUI, which is what I want is for integration purpose.
So how can I run this tool on Mac OS on terminal?

If javac was written in Java, why I can execute javac as if it is a none-java program? [duplicate]

This question already has an answer here:
Why Java compiler as distributed as executable and not as JVM bytecode?
(1 answer)
Closed 7 years ago.
Java program needs to be packaged to JAR file so it can be executed using java -jar command. So why don't I have to execute javac with java -jar javac command? How did Sun/Oracle make java program into executable binary file?
I know there are tools that can convert jar file to windows executable file. But I want my jars to be executable in Linux/OS X without the help of bash script.
---------- UPDATE
I found this link really helpful: https://github.com/maynooth/CS210/wiki/Convert-Java-Executable-to-Linux-Executable
If javac was written in Java, why I can execute javac as if it is a none-java program?
The answer to your question has already been given by Jon Skeet for the question Why Java compiler as distributed as executable and not as JVM bytecode?
Quoting his answer here below
javac.exe (on my installation, JDK 1.8 on Windows x64) is about 15K in size. This isn't the full compiler. The compiler itself really is written in Java, and javac.exe is just a launcher, effectively. This is true of many of the tools that come with Java - it would be a pain to have to run something like:
java -cp path/to/javac.jar java.tools.Javac -cp path/to/your/libraries Foo.java
A simple way to understand the whole thing is imagining JRE(Java runtime environment) acting like an intermediate layer between your program and the OS.
JRE accepts the bytecode and runs your java program.The javac (java compiler ) converts your java source code to platform neutral byte code(exceptions are there). I am not sure if java,javac,jre is written in java or not. But if they are,then they have to linked/loaded in a different way in different OS(platforms).
Now coming to how to run jar in windows and linux
Normally java code is converted to jar file. Then there will be two files(mostly along with jar file) to start the jar file , one for windows and one for linux
For example Apache tomcat has files (in same location)
startup.bat ==> to start program in windows.
startup.sh ==> to start program in linux.
Alternately you could convert jar to exe for windows.
For linux the link you specified is enough. The script is interpreted by command interpreter in linux and your jar file will be executed.
This link specifies different ways of executing a shell script in linux.
http://www.thegeekstuff.com/2010/07/execute-shell-script/
This link has code to run jar as bat
Run .jar from batch-file
To sum it up , your jar file can be platform independent but they way to start the jar file will differ for different platforms.

Can you run a Java program in Linux using the name only, without the "java" command?

If I am writing HelloWorld, is there a way I can run the program from any directory by just typing HelloWorld? Sort of the same way once you set up Ant, you can just run Ant from any directory?
Just for some details, we are creating a CLI based toolkit for a customer, and just curious if we can compile it, install it, and just have them run it using the toolkit name.
You can always create a shell script, call it HelloWorld and make it run java with your JAR.
You'll then need to chmod the script to make it executable, and place it somewhere in your $PATH.
The script would like something like:
#!/bin/bash
cd /path/to/helloworld
java -jar HelloWorld.jar "$#"
or
#!/bin/bash
java -jar /path/to/helloworld/HelloWorld.jar "$#"
depending on your exact requirements.
Common solution for your problem is to create a separate launcher application, which is non-java application that runs your Java program. Launcher can be written in some compilable language such as C/C++ and compiled into native executable. Also it can be written in some interpreted language such as Unix shell, perl, python etc and made executable by adding #!/path/to/interpreter line at the beginning of launcher file and setting executable flag on it. Also there are several utilities that can generate launcher for your program such as launch4j or jsmooth.
On Linux (specifically), you could use the /proc filesystem (see proc(5) man page) and its binfmt_misc (actually the /proc/sys/fs/binfmt_misc/register pseudo-file and other pseudofiles under /proc/sys/fs/binfmt_misc/) to register java as the handler for .class or .jar files. Read the Documentation/binfmt_misc.txt file in the kernel source for gory details.
Then any executable .jar file would be interpreted by java (or jexec)
I'm not sure it is worth the effort. I find that wrapping your Java program in some shell script is much more easy (and more portable, because few Linux systems actually use binfmt_misc, and your customer may need some sysadmin skills to enable it).

Deploying a Java project on Linux system

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.

Categories