I'm starting to program in Java and I can't quite understand how importing libraries works.
Suppose I need to import a library in order to be able to write a specific program. I save it in my PC.
How is someone who does not have the same library on their machine supposed to be able to run the program? This is probably a silly question, but I really am new to programming.. Thanks.
They need a java runtime (JRE) installed.
Third party libraries like apache commons, accompany your application jar.
myapp.jar
lib/apache-commons.jar
lib/ido.jar
The myapp.jar (zip format) contains a METAINF/MANIFEST.MF containing an entry
Main-Class: ...
Class-Path: lib/apache-commons.jar, lib/ido.jar
And with that myapp.jar is an executable jar.
A build tool like maven can be used to build such a distribution: take care of library versions (dependencies), preparing the manifest, generate to output folder.
"Someone who does not have the same library on their machine" is a wrong concept. A machine has to have the Java Runtime Environment (JRE) installed on it in order to run a Java program (i.e., to execute the .class file). The JRE contains all the Java libraries. Therefore, every machine running Java has every library that you can possibly use in your program.
Importing a library in your program is basically instructing the compiler to include that library in the program. You don't really save the Java program on your PC with the libraries in it. It's just a set of instructions. These instructions are given to the Java platform on any machine the program is run on. While running the program, the JRE on that machine uses the libraries as instructed in the .class file.
I hope I could make it clear to you.
In addition to the above answer, have a look at the libraries, which are found by:
right click on project
build path
configure path
libraries
You can check weather the JRE is added to your project or not
If it's not present click on add libraries and add it
Related
I have a couple of python files with some dependencies on third-party libraries, like pyaudio. So is there a way to compile everything including python intepreter itself into one .jar\dll file to use them in java\android or xamarin\ .net core, without actual installing python + doing pip install every time?
Also as an option - compile into c\c++?
You can use PyInstaller to create an executable.
Use this command:
pyinstaller --onefile <your_script>.py
https://medium.com/dreamcatcher-its-blog/making-an-stand-alone-executable-from-a-python-script-using-pyinstaller-d1df9170e263
I do not know about compiling into C, but for compiling executables in general you can use pyinstaller, cx_freeze, or a few other less common modules to create an executable folder which contains all the .dll files to run the program. I only have experience with cx_freeze so I'll discuss that here. If your goal is to have the end-user have only 1 "file" show up when they download it you need to use an installer program. To semi-quote cx_freeze documentation at: https://cx-freeze.readthedocs.io/en/latest/faq.html
cx_Freeze does not support building a single file exe, where all of the libraries for your application are embedded in one executable file. [There are modules that do this, but it's my understanding they use "hacks" that can get them flagged by antivirus software.]
You can use IExpress to compress the build directory from cx_Freeze into a self-extracting archive: an exe which unpacks your application into a temporary directory and runs it. IExpress is a utility that’s included with Windows, intended for making installers, but it works equally well if you tell it to run the cx_Freeze-built exe after extraction.
Alternatively, you can create a self extracting archive using 7zip. This is a bit more complex than using IExpress, but might provide more flexibility, and allows you to build your application using only open source tools.
Alternatively you can compile with python setup.py bdist_msi to create a single .msi file which will let the user choose where they want to install the program. At the end of the day the user will still have a directory with all the .dll files and whatnot, but they get to choose where they tuck that stuff away on their hard drive! I think this is the method most applications I've installed use. This is assuming you develop on Windows as well, if not you should include your OS on your post.
I want to run a java program as an exe in Windows. The windows box doesn't install java at all...
So, is there any other way in which the java program can be converted to an exe which removes the need for a dependency on the JRE?
You can use Excelsior JET compiler for that purpose.
See http://www.excelsiorjet.com/ for more information on this.
You can ship the JRE with your application and use that JRE for your application. The effect is the same: The application will be started through an executable (wrapper needed) or script (batch) file and the target machine does not need to have a java runtime installed.
Java doesn't have to be 'installed', it just has to be 'present'.
For the application to run you will need the runtime. In fact the very first thing that happens when you start the app is a call is a made to OS to start JRE. You cannot do without JRE.
[You can of course embded JRE into your app itself if you want].
I have used JSmooth to exify my application. It also allows for embedding a JRE inside. I just used the "ensure that at least Java X is available".
GPL, can be run as an ant task.
Well given the fact, that you are requesting an executable file (exe) in Windows, there is another approach:
Use IKVM.NET - Bytecode Compiler which converts Java bytecode to .NET dll's and exe's.
Get the latest version of IKVM.NET here.
Use this command
ikvmc -target:exe -out:foo.exe yourJarFile.jar
to create your .NET executable file.
After this, you can use your exe with the mandatory IKVM dll's or if you prefer one exe file, you can use ILMerge in order to get a single executable file:
ILMerge.exe /target:winexe /targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1" /out:C:\foo\bar.exe foo.exe <IKVM dll's>.dll
If you are using JDK 9 and above then you can use jlink. It will include all the necessary modules, header files, security policy files, etc, and build a minimal runtime image. This image can be shipped directly to the customer. You can specify your own launcher and what not.
jlink description:
You can use the jlink tool to assemble and optimize a set of modules and their dependencies into a custom runtime image.
Read more at oracle docs: Java Platform, Standard Edition Tools Reference.
GCJ can create native code from Java source files. Here's a tutorial on how to build it in Windows.
Have you tried install4j? There are various versions, some free, of this concept. Basically, this application compiles your application into an executable installer, specific to the OS of your choice.
Easiest way to do this task is to use the launch4j for the windows exe wrapper and then use inno setup to create the installer. When you are creating the installer for the you application add the folder under the other application files. Make sure jre is inside the folder.
Work done!!!
I want to create a executable jar file of a small game that i wrote in java and make it playable in any machine with simple double click like exe file. My question is do you need to install java runtime first in order for executable jar file to work or can it work on a machine without any java installed as well?
No it can't. However, clients do not require the JDK, the JRE would do.
You can make your jar declare its own main class and have users double click it normally like an exe file or create a bat file in Windows.
It requires a JRE instance to be installed on the host machine. This is because JAR files are actually executed via a command line like (in Windows):
java -jar ...
This means that somewhere on your system the java.exe executable should be reachable, either by including its folder in the PATH (Windows) or replacing java with its full path.
Also, most likely you will need to have all the runtime Java libraries to be hosted on the system, as the JAR file containing the application you want to run is not supposed to contain all the Java API libraries. They are also part of the JRE package.
My explanation is tied to Windows for the sake of examples, but it can be extended to any OS.
Do you need to install java runtime first in order for executable jar file to work?
Yes, of course. To run Java .jar files first you need to have installed at least the JRE (run time environment). The JDK (development kit) is a superset of the JRE and will also work for running .jar files.
Can it work on a machine without any java installed as well?
No, as mentioned above, at the bare minimum the JRE must be installed.
You have to have a Java runtime environment (JRE) available on the machine unless you use a tool that performs ahead-of-time compilation (AOT, which is contrast to the usual Just-In-Time). Such tools are available (such as Excelsior JET), but they have a number of downsides, including cost and the fact that a precompiled Java application is a regular executable and will only run on one operating system. I've seen some installers that will detect whether a JRE is installed and launch the Java installer for the user if not.
Yes ! of course, JRE is required and it is not compulsory for JDK to be installed. Since, the main class is defined in JRE for .jar files, it is necessary to have JRE on your machine. I tried with Windows OS.
Actually you can bundle JRE within your exe file with several java .exe wrappers.
Here are few of them JSmooth, Launch4j, Jar2Exe.
I have exe file called Myapp.exe. Now I want to convert .exe to jar file. That jar file should also work in NON JAVA system. I don't have any idea to implement it. Can anyone please suggest me how to do it?
Direct conversion not available !!!
because they are in entirely different platforms.
Meeting your requirements is impossible for two reasons.
1) You cannot change an EXE to a JAR file.
2) You cannot run a JAR file on a system that doesn't have Java installed.
If you want to run something on a (Windows) system with no Java installation, it needs to be an EXE ... or something else that doesn't require Java.
(It might help if you explained why you think you need to do this. Perhaps there is an alternative set of requirements that are not impossible to meet.)
why i am doing all those stubs is for making my jar has to work in java not installed system.
It needs to be an EXE then!
I have an jar.It is working fine in java installed system.My task is to Bundle jre inside jar(Not along with jar(i.e we can put jre and jar in same folder to run a jar as given in following url mindfiresolutions.com/… ))Because i have to give jar file only to client,in such a way that they can use this Myapp.jar in non java system also.But,i don't know how to bundle jre inside jar.I Don't how to run jre inside jar?
Ermm ...
Is it possible?
No. You cannot embed a JRE inside a JAR file in any way that would allow it (the JAR file) to run your Java code without first installing Java. (And installing Java would defeat the purpose of embedding the JRE ... of course.)
But what you can do is create an EXE file which has a JRE and a JAR embedded in it. And there are tools for doing this. Here's the canonical Question on how to do it:
How can I convert my Java program to an .exe file?
I think you need to read the Oracle documentation on what a JAR file really is, and how Java programs are normally executed. That will help you understand what is feasible ... and what is nonsensical.
Can I include the rt.jar in my executable jar file and double click to run it without installing java on the machine first ? I hope it to use that rt.jar in my jar to start it self, possible ? If not, any other way ?
No, you need a java virtual machine. rt.jar is also interpreted by the virtual machine and is just the java class library.
If you're looking to turn the code native you might consider gcj which can convert java code to machine code and wrap it up in an exe as per gcc. However, I'm not sure what version of java gcj supports - I've read somewhere it isn't very recent.
It looks like Launch4j can include a bundled JRE, so you might consider this.
Java needs a Java virtual machine (JVM/JRE whatever you want to call it) to run java applications. However, I'm not so sure you need Java to be installed, rather it just be present.
You could provide this alongside your JAR (with a batch file to run the JAR files - as there won't be associations if it's not installed) but it would seriously expand the size of your project.
There are licensing issues to address too.
The file rt.jar has Java classes in it (in the form of Java bytecode). In order to interpret the classes, you will need to have Java installed.