954,219 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Running Java Program outside an IDE

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.

vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
 

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... :)

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

javaw is designed for that (launches the JVM but without a command window) again needs to be in your path. Then create a windows shortcut with the necessary command. See here for more info:

http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/java.html

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

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.

orko
Junior Poster
164 posts since Apr 2006
Reputation Points: 46
Solved Threads: 11
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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).

schoolsoluction
Light Poster
27 posts since Sep 2007
Reputation Points: 30
Solved Threads: 3
 
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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
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?

vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
 
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.

vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
 

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).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
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.

vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
 

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... :)

orko
Junior Poster
164 posts since Apr 2006
Reputation Points: 46
Solved Threads: 11
 
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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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.

cms271828
Junior Poster
123 posts since Oct 2006
Reputation Points: 20
Solved Threads: 4
 

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.

schoolsoluction
Light Poster
27 posts since Sep 2007
Reputation Points: 30
Solved Threads: 3
 

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.

vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
 

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\\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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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\\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.

vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You