new to java - understanding java compilation - java

Java programs can be compiled into an .exe then someone can click on this .exe icon and run the app. My questions are:
Is that the only way a java program can be compiled and ran?
Is it possible to just write a class.java file, put this file on the
server, and expect it to "start working" without any kind of exe?
Are there other ways to compile java programs other than creating an
.exe or .apk file (Android)?

java class is compiled to form a .jar file and not an .exe file
A Jar file is essentially a collection of files (mostly archives of java classes) in one file, similar to a ZIP file. A Jar file facilitates the packaging of applications into one file. In addition to the Java class files and resources, a Jar file can contain a META-INF directory. The most important file in this directory is manifest.mf. It is used to define extension and package related data. The Main-Class: classname in the manifest file specifies the class that gets executed first.

A way I run text-based java programs is I make it into a .jar and then excecute it in CMD using java -jar nameOfJar.jar

Java applications are usually distributed as runnable jar packages, not exe where all java files are compiled into class files. You must consider that exe files are executable on Windows platform, but not in *nix, Solaris, etc.
So you can create .jar file. It runs on every platform which have installed JRE.
For android, you can create .apk file and that is it. Not an exe file!

Java programs are compiled to .class files that run on a Java Virtual Machine. The way to execute a Java Virtual Machine depends on the Operative System you are using.
Also .class files can be packaged on .jar, .war or .ear files.

At first, JAVA programs are not compiled to .EXE, they are compiled to .CLASS files which can be packaged into a .JAR files. You can simply distribute the .JAR files which can be directly executed when the system has JAVA Runtime installed.
Also please note that .EXE files are Windows Executables and cannot be run under other operating system environments but there are tools that convert .JAR files to .EXE files such as JSmooth and Launch4J.

Related

Java exe or binaries creation [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed last year.
Is there a way to create exe files or binaries in java? I've been only able to create java.class files after compiling the code, but I was wondering whether it is possible to create a normal program in a binary or a exe format that I could run without using command java File every time I want to run a program.
The suggested Executable jar files still require a java interpreter to be installed on the system. And as mentioned above, compiling directly into a Windows Executable looses platform independence. Yet it may be desireable to get a more native look and feel during application installation.
For this Oracle/the Java community created JPackage. It wraps your application together with the required JVM such that the whole package can be treated like a native application - regardless whether you want to run on Linux, MacOS or Windows.
You can package all the class files into a .jar file. This .jar file is executable by running java -jar <file>.jar, but most operating systems will allow you, when Java is installed, to double click on the jar file and execute it in this way.
Most build systems (such as maven and gradle) will make it easy for you to create such a file. If you have external dependencies (other jar files), you will need to create a "fat jar" that also includes those dependencies; there are plugins for those build systems to create fat jars.
You can use Launch4J to make an exe file, here you can find more info - https://youtu.be/jPKxqc8Zg-0

How to run executable jar in other systems?

I have scripted a selenium web driver program in java on eclipse IDE.
This program gets the data from a excel sheet placed on my systems desktop and all the jars are added in the build path from my desktop.
EXAMPLE:C:\Users\PEOPLE\Desktop\selenium-server-2.53.0\selenium-2.53.0\libs.
The entire program is exported into a runnable jar file and placed on the desktop.
The jar file is working fine as expected.
Now the question is how to make jar file run in other systems?
what are the pre requisites to make jar file run in other system?
I see two things mainly:
A Java JRE installed so that you can run java -jar yourJar.jar or java -classpath yourJar.jar your.package.name.MainClass
A way to configure the path for loading your Excel sheet (specially if the path is different on the other system). For instance, you could read this path in your Java program from a configuration text file app.config located in the same repository of your jar.
Hope it helps :)
Create the same path for the Excel file as is in your system (else you need to edit the Config file) along with the name of the file.Now, just install the latest JRE in the system and you should be good to go.

Running a Jar File in a environment without Java [duplicate]

This question already has answers here:
Running java without installing jre?
(8 answers)
Closed 4 years ago.
I made an executable jar file but I want to distribute it to users who may not have Java installed on there computers (Mac or PC). Is there anything I could do anything on my part as the programmer to make sure people without Java can run?
Other than 1) asking them to install Java, or 2) writing your own JVM, the answer is generally no. You have to have a JVM/JRE for your jar file, unless you have a development environment that can create a native executable from your code. But, then it won't be a standard jar file - it will be a native binary (if this development environment just bundles a JVM of some sort into package containing your jar file, with a small executable stub - this would still be putting JVM on their machine - it would just be a bit hidden from a user). So unless you can generate a native binary (not a jar file) from your source, no.
Same with writing something in .NET and attempting to execute it in an environment that does not understand what .NET is, writing something that requires a Python interpreter and trying to run it in and environment without Python, etc.
Running java without installing jre?
Create a folder(lets say PROGRAM) which include folders bin and lib, of your installed JRE.
In my computer I can find them at this path:
C:\Program Files\Java\jre1.8.0_25
Then with Launch4J create a JAR or Exe file of your program inside that containing folder(PROGRAM). Also when you create this file you need to manually select root to these bin and lib folders.
Then user dont need to have JRE installed, hovewer he needs to have folder with program and also bin and lib files in it.
If my english is not enough and these type of solution is what you looking for then heres another source...
How to bundle a JRE with Launch4j?
No need for Launch4j or any other software. Convert your project to .jar file in eclipse then after that put your installed JRE folder in the same folder where your .jar file exists.
Uninstall JAVA from your system then go to JRE folder in that bin folder then open command prompt.
Type:
java -jar ..\..\fileName.jar
i.e java -jar ..slash..slashfileName.jar
Make sure that the JRE installed file and your .jar file is in the same folder.

Difference between running jar file and exe?

If you have a small program, you can run jar file and it will work fine. But if you convert jar file into exe, you still need java to run your exe file, so what's the difference between them and why do some people convert jar to exe?
An EXE is, ostensibly, an executable program that launches the local java to execute the bundle classes.
As you may know, on your computer you can associate certain file extensions with local programs. For example, .doc files with your word processor.
Similarly, .jar files can be associated with Java, so that Java can execute them. The jar file is considered "stand alone" if it has all of the necessary classes bundled within it, and a proper manifest pointing to the startup class.
So, by associating .jar with Java, clicking on it in your environment will launch Java with the given jar file.
An EXE doesn't need that association. It find java on its own with it's own launcher.
The next step is that you can actually bundle the JRE in to an EXE, so you don't even need to have the user install Java as a pre-requisite. But that's a different process.
People commonly use Java executable wrappers for two reasons - 1. to simply deployment for environments without a JVM, and 2. To make sure the exact Java runtime used for developing the application gets used to run the JAR. However, the practice is not that much widespread.
Java archive or jar is an archive of compiled java byte code and resources which can be run on a java virtual machine. ".exe" is a windows extension for directly executable code mostly used by installers or programs that do not need to be installed. I think your "people" are talking about installers.
An Exe file is an executable file that can be executed in Microsoft OS environment.
Jar file is container of Java Class files, including other resources related to the project. Jar file can be executed only if Java run time environment.
The JavaTM Archive (JAR) file format enables you to bundle multiple files into a single archive file.
The .class files compiled from java files, can not be launched directly. That is why it is needed to be converted to exe before it can run in a windows environment.The usual way to start a java program by batch file is not a convenient way. So inorder to avoid this difficulty we need to convert jar files into exe file.
Also converting it to exe. enables the program to run by simple double click on the program, instead of having to compile it with an IDE or through the JVM.
All that the exe will do is to start a jvm with your app, something like this: "java -jar app.jar".

Make File to run on Linux/ MAC OS to compile .Java Files to an exe form

I am a beginner in Java. I made my Java program using NetBeans. Now I have my .jar file.
But I would like to know how to write commands to compile .Java classes to .Class and then build a jar using those .Class files.
Does any one have an idea how to create a Make file/ Script for running on Mac to run my source code by compiling it itself.
I would use maven This will download dependencies, build you class files and build a jar for you.

Categories

Resources