When to use a Java GUI [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert my Java program to an .exe file?
Compiling a java program into an exe
I believe this is an easy question, though I cannot find a quick answer.
I've been learning in a Java class about GUI making (and really Java coding in general) which, when compiled, creates a .java file. That .java file can be opened with some sort of IDE but not just opened like a .exe file.
This brings me to my question. When/where do you actually use a Java GUI? I don't foresee me passing out .java files for my friends or co-workers to use the tools I've made. Are they supposed to work best with online applications?

Your .java file contains your Java code, which you then compile to a class file. That could be executed by your friends.
In most cases you would bundle your app with its resources in an executable jar file. If configured correctly, the operating system can run the Java app just by double clicking on it. One other solution would be to use Java webstart to distribute your GUI app but that is essentially the same thing. Java and the file association to jnlp files have to be configured correctly.
The easiest way is probably to use an exe wrapper like launch4j to create an exe file that loads the JVM and runs the Java application. Those wrappers can even create distributions with bundled jvms to make sure your application is able to run if the user doesn't have Java installed.
Hope it helps.

A .java file is source code which is meant for a programmer to write code in a format suitable to them, to define the behaviour of the program.
Source code is compiled into .class files which you can execute (run), although they are often packaged into JAR files (which are simply collections of .class files).
These can be executable and can be passed between people to share programs.
GUIs are irrelevant. You can either create a GUI or not, but the functionality will still exist in the code and can be executable. GUIs allow you to view and interact with a program, whereas without a GUI you have to use the command line if interaction is required.

When Java is compiled, it creates .class files, not .java files. The .java files are source code; the .class files are the result of compilation.
.java files can be opened by an IDE, but it doesn't make sense to open them as an .exe file because, again, .java files are source code.
To run Java applications, you have to run a Java Virtual Machine which is specific to your operating system. You provide the .class file as input, and the JVM runs it.
The .class files are the Java-equivalent to Windows .exe files, in the sense that they are "executable". However, while .exe files can be directly executed by Windows, .class files aren't directly executable by any operating system but by an operating system-specific JVM.
The "strength" of Java in that respect is that the same .class file (executable) can be run on any operating system that has a JVM installed on it.

It really doesn't matter if you develop a Java GUI (graphic user interface) or a Java CL (command line) program as in the end your software is supposed to run just like an *.exe as long as the user have the "Java Virtual Machine" installed.

Related

How can Java run a PE32 when passed to its class path?

I've been wanting to reverse engineer this clients launcher in an effort to understand how the game was launching as a Java application despite it only having a PE32 executable alongside it.
The launching of the client goes as follows:
java -Xmx384M -Dfile.encoding="UTF-8" -cp TargetBinary.exe com.java.client.Client
Now I was curious what TargetBinary.exe actually was, as this was being ran on a *NIX system. Running file I observed this output.
TargetBinary.exe: PE32 executable (GUI) Intel 80386 (stripped to external PDB), for MS Windows
The second part is the com.java.client.Client after the TargetBinary.exe, this stands out as a Java pathing which Client is my target.
Here are my questions:
How can Java add the TargetBinary.exe to its classpath?
As a followup, are there any recommendations to decompile it to the point where I can observe Client and more so understand how it was all packed together?
Being a Portable Executable (PE) the libs, code, etc should all be there inside the TargetBinary.exe and somehow Java knows what to do with it?
Zip files are read from the back, so it's easy to put a zip (jar) file at the end of an executable file, and have it work as both.
See https://en.wikipedia.org/wiki/ZIP_(file_format)#Structure
Try running jar -tvf TargetBinary.exe to see the names of the classes and resources in the jar. If you make the file a dependency of a project in your IDE you can see decompiled code and navigate around the project, and run it and set breakpoints.

Make a .jar file that "just runs" everywhere?

I've written a few java applications (desktop, no browser involved) for friends, some of whom are less technical than I am. They use Windows, Linux and Macs. Originally I just used javac to generate bunches of .class files, zipped it all up and had them unpack it all. It works, but they had to install the JRE (terrifying for some), write a script to run java or javaw followed by command line args, app name, more arguments (incomprehensible for some), mark the script executable... I got mocked, somewhat rightfully, for a geek solution. They wanted to download from a website, doubleclick and be up and running.
I thought creating a .jar file would be the solution. It didn't help. In Eclipse, the options seem to be "create a jar file", which lets me include the handful of resources (.png files mostly) the apps needs, but the result isn't runnable from a command line. Or, create an executable .jar file, which doesn't seem (at least from Eclipse) to have a way to include resources - and the resulting .jar file doesn't start when double-clicked, even when I set the execute bit, even though the default run environment points to the Java suit. Even with a .jar file, I'm stuck with having them script "java -jar App...", and that's pretty much a dealbreaker.
I'm missing something. The point of Java is platform independence. Is there a platform independent way to have them download a single file, double click it and have it off and running?
If you are trying to create a runnable jar file from your project in Eclipse, you have to select that option when exporting. You can do this by Export > Java > Runnable Jar File. If you only select the Jar File option, you won't be able to run it.
If you want to wrap your application as a .exe, you can use launch4j. Here's a thread that explains it a little more and has some other options.

How to make java executable in linux with it's files?

Well, i have the folder of the java program that i made with eclipse and it contains the .java and .class files along with some folders that contain images and text files needed for the proper program's operation. My question is, how can i make a whole executable(.sh in this case) program, that works exactly as i have compiled it run it? For example, i let people download it and i want them to run it just by typing ./(filename).sh .
Since i wrote the program on linux, and i have to test it on linux at the moment, how is possible to make the exact same work on windows machine?(i will transfer the eclipse project)
Thanks in advance.

the .jar file does not execute when I click on it

I know this question would sound very stupid, but this is my first java program after all.
K, the question is, when type this: java -jar JavaApplicationTest.jar the program executes without problems, but when I go to the folder where the JavaApplicationTest.jar file is, and click on it, it does not execute. I thought that .jar files were like .exe file, are they? I mean, in the way that we click on them and the program runs; 'cos the java virtual machine is running in the back ground.
Please, any help would be very much appreciated.
No, they aren't exactly like .exe. Only .exe is an .exe.
When you execute a JAR file, it's the Java JVM that's running, not your JAR file. The JVM opens the JAR, loads the .class byte code, and executes the main class that you specified in the META-INF.
I'm guessing that your Windows operating system is doing something besides running the JVM when you double click. Try right clicking and seeing what options your Windows operating system presents to you. If one of them is to unzip the file, you'll have to add running the JVM as another choice.
The jar-file runs. But there's a big difference between:
java -jar someJar and someJar.jar/respectively double-clicking on the jar.
The full command launches the jar in the same commandline, in which you have entered the command. Double-clicking on the jar creates a process aswell. But with a completely separate console-window that is hidden.
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.

Java and windows? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Compiling a java program into an exe
How can I convert my java program to an .exe file ?
hi everyone . there are many programs such intellij idea or jedit which they have written in JAVA ,
How they have compiled the code into the exe file ?
How can ,I compile my java code to exe file and run it on windows?
And Why javac give us class file ? what is it ?
how about searching stack overflow:
How do I create an .exe for a Java program?
also, like the answer points out launch4j is the program you want to use. i have used it and it works well.
mkoryak took care of how to create an .exe for a Java program, but let me address "What is a Class file"?
In general, you don't have to compile Java down to byte code in the form of an exe (or anything else). You compile .java files into .class files. These files can't be run by an OS directly - rather, they are run by the Java Virtual Machine (JVM). The JVM is essentially a layer between your Java program and the underlying OS. The JVM will, on your behalf, interpret your .class files and pass the instructions contained in them to the underlying OS (Windows, OS X, Linux, etc).
So what does this do for you?
For one, it makes Java a little slower than other languages that are compiled right down to byte code. This was initially a big knock against Java but, as JVM interpreters have gotten better and people have migrated to slower platforms (i.e. Web Apps), this minor latency issue has become less and less of a problem.
On the flip side, however, you can write your Java code for a single OS - the JVM. You don't need to write one version of your app that targets Windows and another that targets OS X, and another, and another... Rather, you write one version of your application and you target that single OS (the JVM). The fact that there are versions of the JVM for every major OS out there means that, as soon as you write a Java program, you can run it on any platform (in theory, anyway).
That's one of the really cool things about Java, and also the meaning behind the often-heard tagline: "Write once, run anywhere."
So that's what the .class files are all about.
In Java, you write .java files and use a Java compiler (such as javac) to create .class files. These .class files are then run by a Java runtime (such as java), which executes your program on the JVM.
I hope that helps and, maybe, you don't need to compile your Java program down to an exe, at all.

Categories