Hi everyone, I need to be able to run a Java application (not an applet) directly, that is not from an IDE or the command line. How can this be done for a Windows PC? I have seen many applications written in Java, but are executed just like other Windows apps, so there must be a way.

Recommended Answers

All 17 Replies

In your command line prompt type java myProgram to run a compiled version of a java program called myProgram. You may need to set an environment variable for the path where your java vm is installed. Let us know if you need to know how to do this... :)

To compile the code you have to type:
javac myapp.java
it will create a myapp.class file in the same or diff location... then just follow it and type java myapp it will run the code... you have to compile before running... you may have to setup the classpath to include your JRE in your environment... you can follow this:
go to Start menu-> My Computer (right button click) select Properties -> Advanced ->Environment Variables-> CLASSPATH click on Edit, and set the path location of your jre... typically it is under Program Files/Java/jre1.x.0_xx here x is some number.

Once you install Java, you should be able to "double-click" on a .class file and execute it in that way, if you don't have a class file (but just a .java file), then you have to compile it/them first. Better would be, to create a jarfile containing the relevant classpath info and such in the Manifest file so that every thing is handled correctly, and then "double-click" that jarfile.

Obviously.

Better would be, to create a jarfile containing the relevant classpath info and such in the Manifest file so that every thing is handled correctly, and then "double-click" that jarfile.

Obviously.

Best is to use ANT to build your projects to jar files. (or maven if you want to take it a step further).

Best is to use ANT to build your projects to jar files. (or maven if you want to take it a step further).

Once you know how to do it manually, yes. But just like with using an IDE, you should not start with it. You should first learn how to do it manually, so you understand what is happening.

That said, what real difference does it make what tool he uses to build the jarfiles (once he understands what is happening, anyway)?

Making a jarfile was not suggested only for convenience (which the ANT suggestion was). It could be, and probably is, a very real necessity, if any third party libraries are involved.

Once you install Java, you should be able to "double-click" on a .class file and execute it in that way, if you don't have a class file (but just a .java file), then you have to compile it/them first.

No, it is not so on my system. The .class does not have an association with any program and isn't opened. I have to select the program I want to open the .class file with.

What I'm actually looking for is a normal windows EXE file, which when executed launches the Java application. Maybe I can use something like

system("java app.class");

in the code. But then how do I get the right path for Java.exe?

Better would be, to create a jarfile containing the relevant classpath info and such in the Manifest file so that every thing is handled correctly, and then "double-click" that jarfile.

Well a .jar file open with WinRAR on my system! It seems that jarfiles are like a collection of zipped .class files.

You have done something that has changed the file associations on your machine. (For the jarfile one it was the installation of WinRAR.)

Change them back.

.class should be opened with "java" or "javaw" and .jar should be opened with "java -jar" or "javaw -jar". Alternatively, you could de and re install java and let it reset the file associations itself.

Edit: And java.exe (or javaw.exe) should already be in your path if java is properly installed. If not, add the directory containg them to your path, obviously.

All of these things, BTW, are System Maintenance questions, and not Java questions, per se.

Edit Again: All of these, BTW again, would also be non-issues, if you had learned Java the right way, rather than learning it using an IDE (which is really only learning to use the IDE and not learning Java, as illustrated by these questions).


Edit Again: All of these, BTW again, would also be non-issues, if you had learned Java the right way, rather than learning it using an IDE (which is really only learning to use the IDE and not learning Java, as illustrated by these questions).

Yes, I know all about javac and java and compiling and executing Java programs... Thats really the first thing I learnt. But I wouldn't expect the end users of my application to go to the command prompt to execute it... They should be able to do that just like ordinary programs. See one of my earlier posts to see what I mean.

oh... now i understand... :-S... so hopefully u r not giving ur java files away... u can write a batch file for ur windows users and a shell script for ur unix users... they can execute both by just double clicking... it shud solve ur problem... :)

Yes, I know all about javac and java and compiling and executing Java programs... Thats really the first thing I learnt. But I wouldn't expect the end users of my application to go to the command prompt to execute it... They should be able to do that just like ordinary programs. See one of my earlier posts to see what I mean.

If they install Java properly, and you provide them with a jarfile all they need to do is double click on it. No muss, no fuss. The only problem is third party libraries, but that's why they make installers, and there are all sorts of free ones to choose from.

Edit: Then again, if you use an installer, you can have it install Java as well, so that is also a problem solved.

If you use an IDE like eclipse, you can JAR it there, by right clicking on project, then 'Export'

If you want to convert into an exe file, its probably easiest to use a tool like...
http://mpowers.net/executor/

The trial version of this always opens a command prompt, and doesn't let you change the icon, but the proper version does not show command prompt when you run, lets you change icon, and lets you package a JVM into it incase the OS the exe is about to run on doesnt have java installed.

If you want to distribute the JAR file in a 'nice' way (splash screen, easy installation, creation of shortcuts with your own icon) you can distribute it through 'java web start'.

That gives you the possibility to distribute the installation through the web. And you can update your program very easily on all stations by putting new versions of your jarfiles on the webserver.

Thanks everyone, I now know many ways to distribute my Java apps... jar files, batch files, web start or exes'. You guys are really helpful...

One more question... Is there a standard registry entry in Windows that tells me the path of Java.exe? Just so that I can tell whether Java is installed on a particular machine.

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\Current Version

Will give you the current version and

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\<current version>\JavaHome

Will give you the jre directory. Check up one level from that for a bin/javac to determine if it is a jdk. If that does not exist, then it is only a jre.

Edit: This is, of course, Windows only, which makes it paltform depedent. To try, first for platform independence, try searching for a jre or java on the path from a System.getEnv("PATH") call, but it is, obviously, not guaranteed to be found there. Otherwise, you can use the rpm command to find it on Linux, and pkginfo on Solaris. Other OSes, I couldn't tell you. None of these, however, are truely satisfactory.

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\Current Version

Will give you the current version and

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\<current version>\JavaHome

Will give you the jre directory. Check up one level from that for a bin/javac to determine if it is a jdk. If that does not exist, then it is only a jre.

Edit: This is, of course, Windows only, which makes it paltform depedent. To try, first for platform independence, try searching for a jre or java on the path from a System.getEnv("PATH") call, but it is, obviously, not guaranteed to be found there. Otherwise, you can use the rpm command to find it on Linux, and pkginfo on Solaris. Other OSes, I couldn't tell you. None of these, however, are truely satisfactory.

Thanks again... The registry keys were exactly what I was looking for. Well my app is required to run only on Windows, so platform-independence is not an issue right now. I chose Java simple because it is easy to program in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.