View Full Version : Java.exe
DELOCH
06-23-2006, 05:31 AM
Hi, I have a tiny question...
How do I compile java not as bit code: .class
but as an executable/executive file(machine code)
It is just that I don't even know how to use it to get .jar...
anyways thanks
oracleguy
06-23-2006, 07:00 AM
Well from my understanding of java, you can't compile it into machine code. It compiles into a byte code that is ran by the JVM on the platform you are running it on. But I'm no means a java expert.
_Aerospace_Eng_
06-23-2006, 08:25 AM
You can use .bat or .com files to make dos programs that actually launch and run the java using command line codes inside of them.
Spookster
06-23-2006, 10:50 AM
Well your terminology is a bit off. If what you want to do is to be able to double click a file in java to execute it you can use a .jar file. Sun has plenty of information on creating an executable jar file
http://java.sun.com/docs/books/tutorial/deployment/jar/
Roelf
06-23-2006, 11:31 AM
but if you create an executable jar file, the user of your application still has to have the JVM to execute it.....
I think that is what he/she tries to avoid
Spookster
06-23-2006, 03:23 PM
but if you create an executable jar file, the user of your application still has to have the JVM to execute it.....
I think that is what he/she tries to avoid
That's kind of the point of Java. Compile once run anywwhere. You would still need the JVM/JRE to run the application whether you use a jar file or not. There are programs that can create .exe files for java but regardless you still need the the VM installed in order to run it.
DELOCH
06-23-2006, 10:13 PM
yes but how do I do this? I can't say: jar filename.class
...
and can I add an icon to it?
Spookster
06-23-2006, 11:31 PM
yes but how do I do this? I can't say: jar filename.class
...
and can I add an icon to it?
Well you need to include all the class files in your jar file as well as create a manifest file. In the manifest file you will specify which class is the main class that needs to run when the jar is executed.
Here is a fairly simple tutorial
http://neptune.netcomp.monash.edu.au/JavaHelp/howto/jar.htm
DELOCH
06-24-2006, 04:59 AM
Uhh... can you give an example for :
class TestApp
{
static int a,b;
public static void main (String[] args)
{
a = 1;
b = 2;
System.out.println("a = " + a + "\n");
System.out.println("b = " + b + "\n");
}
}
Thanks
daniel_g
06-24-2006, 06:07 AM
I don't think there's an easier way than what the tutorial tells you to do, unless there is a program out there that will do it for you...
Open Command Line (Start--->Run--->cmd)
choose a file name
type: jar CF example.jar One.java Two.java Three.java
where "example.jar" is the name you want to give, and "One", "Two", "Three" are the java files that you created. In your case they would be replaced by "TesApp.java"
Make sure Command Line points to the directory in wich you have the .java file.
Spookster
06-24-2006, 06:13 AM
Uhh... can you give an example for :
class TestApp
{
static int a,b;
public static void main (String[] args)
{
a = 1;
b = 2;
System.out.println("a = " + a + "\n");
System.out.println("b = " + b + "\n");
}
}
Thanks
Ok first you need to create a manifest file. Create a text file and in that text file put this exactly:
Main-Class: TestApp
Make sure you put a carriage return in at the end. So after TestApp hit the enter key. The manifest file must always end in a blank line. Then save that text file as manifest.txt and place it in the same directory as TestApp.java. Now in a command prompt compile your TestApp.java if you haven't already so that you have TestApp.class. Now you will create the executable jar. In the command prompt make sure you are in the directory with your files and type this:
jar cmf manifest.txt TestApp.jar TestApp.class
Now you have an executable jar file. You can run it one of two ways. Either from the command line like so:
java -jar TestApp.jar
or by double clicking on it in windows explorer. This is assuming you have associated jar files to run with the java command just like it explains in the tutorial I provided a link to.
DELOCH
06-24-2006, 06:14 PM
it says failed to load class manifest :(
Spookster
06-24-2006, 07:40 PM
it says failed to load class manifest :(
Well which instructions did you follow? If you followed mine exactly it will work just fine. I tried it myself before I posted just to make sure. If you got an error saying failed to load class manifest it sounds like you typed something wrong because it sounds like it is looking for a class named manifest and not TestApp.
Extract the manifest file that was generated from your jar file and look at the contents.
DELOCH
07-07-2006, 03:27 AM
I can simply enter it with FAR which is dos-like editor which owns in a lot of ways... old but fully functional :d
and yeah I tried, the jar file got made but it does not exe
Spookster
07-07-2006, 03:36 AM
and yeah I tried, the jar file got made but it does not exe
That's because you need to create a file association between jar files and the java command to execute it. That was all explained in the link I provided.
http://neptune.netcomp.monash.edu.au/JavaHelp/howto/jar.htm#RunningAnExecutableJARFromExplorer
morongo
07-07-2006, 10:08 AM
If you really want to turn your bytecode into a native binary (self contained exe), here's a list of bytecode compilers that do that:
http://schmidt.devlib.org/java/native-compilers.html
DELOCH
07-07-2006, 06:34 PM
I tried, it didnt execute :\
sage45
07-09-2006, 04:55 PM
Hmmm... RTFM????
-saige-
ghell
07-10-2006, 09:11 PM
With sun's compiler java compiles to bytecode .class files to run anywhere, there is a GNU Java compiler (gjc) to compile to native code and there are other native compilers such as IBM's though most aren't free. The native compilers are very very limited and are usually 2 or 3 major versions of java behind sun's (gjc is at like 1.3.0 or something at the moment im not sure.. but awt and swing arent even supported in it [though swt is] also you need to do a fair bit of work to get this compiler working but i have it working on mingw5.0 but its not really worth it there are no speed improvemenst etc.
executable jar files should be runnable by anyone with java runtime environment installed by simply double clicking the icon (or running in any other way that normally runs a program). They are normal jar files with a custom manifest file which has a Main-Class line in it. However, my computer could never run these by double clicking on them so im assuming not everybody elses can either. You can make a batch or shell script to run the class files (or the executable jar from command line) when clicked but this requires prior knowledge of where java.exe is on the clients machine. even with executable jar files the user will need an appropriate JRE to be able to run it.
One alternative is to use Microsoft's C# which is pretty close to java but compiles to .exe (not native .exe but you can run them by just double clicking) these also run on linux via the mono project if portability is required. the Microsoft .NET Framework is required to compile and run these (so end users must have this installed too) afaik the .net framework is all thats needed to compile as it has csc.exe in it for example but most guides say you need .net framework SDK but i dont know why :)
DELOCH
07-11-2006, 12:20 AM
yeah I understand thanks :D
but there is still a huge problem...
and I quote:
java -jar Java.jar
Failed to load Main-Class manifest from
Java.jar
what do I do, i did what you said exactly :( my compilers are at top shape :(
Thanks for taking time off your busy schedule to help me :D
Spookster
07-11-2006, 12:44 AM
Well attach your jar file, .java file and manifest file and post the exact command you used to create the jar file and the command you used to try and run it.
marek_mar
07-11-2006, 01:34 AM
I have never so much as transfigured a Hello World java code into a jar but for no apparent reason, even without trying to search I found (stumbled upon) this: http://launch4j.sourceforge.net/
DELOCH
07-11-2006, 02:32 AM
ok it works now... if i do java -jar myClass.jar
but, otherwise when I double click, nothing happens...
is this because it only works on framed java(eg: the application ones with GUI?)
I really hope someone answers it so I can close the topic and not bug anyone anymore about JAR(java archive :D)
note: it works with Frames... I wish I knew how to set icon but this is enough for now :D
Thanks again, in advance :D
Spookster
07-11-2006, 06:05 AM
If it doesn't execute by double clicking the jar file icon then that means you haven't properly associated jar files with the java command to run them.
rpgfan3233
07-11-2006, 06:31 AM
If it doesn't execute by double clicking the jar file icon then that means you haven't properly associated jar files with the java command to run them.
To add to that, archiving applications such as WinRAR associate JAR files with that application, so you could in fact be opening a Java ARchive to view its contents rather than running it as a Java executable.
DELOCH
07-11-2006, 01:12 PM
thanks but i set the open to "C:\j2sdk1.4.2_11\bin\javaw.exe" -jar "%1"
but nothing happens... it tries to open a second then not even an error shows up, nothing :(
what do I do?
note: rpg what do you mean?
ghell
07-11-2006, 01:33 PM
If it doesn't execute by double clicking the jar file icon then that means you haven't properly associated jar files with the java command to run them.As i said mine is associated properly but i get told it has no main class when it does (i can run it from command line without telling it what is the main class for example but cant double click it.. it works on other people's computers too)
launch4j is a simple wrapper to make it an exe to make it easier to run so that might be a good idea to just make it easier to use, i used to use it a lot. it just fowards stdin stdout and stderr to and from java but you need to make executable jars before you use launch4j anyway :D i know this wasnt the question but jre is still needed to run launch4j wrapped apps if anyone is wondering ;)
rpg is correct in that as its just an archive format, some software such as winrar may associate to open it in the same way in which it opens a zip or rar or arj or whatever if that explains it any better :)
DELOCH
07-11-2006, 01:48 PM
I know, but i did everything in all those links you gave me and still the same thing, jar files cant run in command prompt for me
I dont want java to run as an exe(unless there is a command for that)
I want to use executive java archives...
can you help me do that? I am seriously confused :(
note: gshell did you just say it does not jar because it says that? did you remember that a manifest file has one line of text and one line of blank? press enter after you done the Main-Class: something<enter> :D
rpgfan3233
07-11-2006, 04:32 PM
Simplest command line:
java -cp . -jar YourClass.jar
That should work. That tells the JRE to look for the classes in the current directory, which is the '.' directory. Essentially what it does is it sets the classpath (cp) for the session that the JRE is open.
DELOCH
07-11-2006, 04:38 PM
I mean from explorer so you doubleclick jar.jar and open the program in the command prompt
Thanks for trying though I appriciate all of you taking time to help me :)
Spookster
07-11-2006, 04:47 PM
thanks but i set the open to "C:\j2sdk1.4.2_11\bin\javaw.exe" -jar "%1"
but nothing happens... it tries to open a second then not even an error shows up, nothing :(
what do I do?
note: rpg what do you mean?
What is it that you are trying to accomplish in your program? If you are just printing a simple string to the screen and not pausing the program or anything of course you won't see anything. It will quickly open a command prompt print the string and then close, often before you can see it.
rpgfan3233
07-11-2006, 05:29 PM
It does not seem to work for me either. I recommend learning how to create your own manifest file and then adding that to the JAR file instead of the default manifest file because JAR cannot tell which class is the main class.
Example manifest file:
Main-Class: YourClass
<new line here!!!! #REQUIRED>
According to the Sun Java forums, there absolutely must be a new line at the end, but you cannot skip lines for other declarations.
Manifest file tutorial: http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
Once you have that, I also recommend changing "javaw.exe" to "java.exe" because javaw doesn't work for console applications. java works for both console apps and Swing/AWT apps.
DELOCH
07-11-2006, 06:32 PM
I am trying to:
make JAR File when double clicked, open the program in the command prompt ... it works amazingly on frame applications, but the console applications try to open but nothing happens :(
and I know how to make all that stuff, all I need is help for how to make my console applications valid programs(double click open)
TheShaner
07-11-2006, 06:39 PM
I think Spookster had it right when saying that your console application opens, executes, and then closes. There's nothing keeping the console open. Try adding in your program a spot to read input so that the console must remain open in order for you to enter input. See, executing a console application without already having the console open will result in it closing itself if the program is no longer running. If you open the console ahead of time, then it will not close itself after the program finishes running.
-Shane
DELOCH
07-11-2006, 06:42 PM
the thing is I know that it opens and closes... but it doesnt even open in the first place... it would at least blink in front of my eyes, i am not blind yet :)
Thanks though :D
rpgfan3233
07-11-2006, 11:47 PM
Do you still get the error saying the main class is not found?
If so, it is because your manifest file doesn't tell the JRE where to start in your application, because you didn't edit the manifest file and you didn't include your own.
Contents of a simple JAR file:
SomeClass.class
META-INF\
META-INF\MANIFEST.MF
Process:
1) You have a class file (SomeClass.class)
2) You have your own manifest file (manifest.in or whatever you want to call it) that declares what the main class is.
3) Command line: jar cmf manifest.in someJar.jar SomeClass.class (creates a Java ARchive with a manifest file "manifest.in" and the output filename "someJar.jar" containing the class SomeClass.class)
4) Test it: java -jar someJar.jar
It works.
DELOCH
07-12-2006, 01:37 AM
I am not getting any errors! it is the best it can be, it runs perfectly when I do java -jar myfile.jar
but it only runs from explorer when it is a frame-based application :\
rpgfan3233
07-12-2006, 03:08 AM
"C:\j2sdk1.4.2_11\bin\javaw.exe" -jar "%1"
Change "javaw.exe" to "java.exe" and that should fix it.
DELOCH
07-13-2006, 03:30 AM
I did that... like i said it all works except the cansole applications :\
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.