I recently installed a java software in my PC and started writing some simple programs. I didn't face any problem while compiling the programs, but while executing it, it shows this error message -
"Windows can't open this file.
File: HelloWorld.java
To open this file, windows need to know what program you want to open it. Windows can go online to lookup it automatically, or you can manually select from a list of programs that are installed on your computer.
I know all the path settings are correct. In fact, there was no problem at all while compiling the program. What can be the problem of this? I even reinstalled JRE and that didn't help. Can someone help me?
Note: I'm using Windows 7 64 bit architecture OS and I'm using command prompt for compilation and execution of the file.
I'll assume that you've double-clicked the .java file, e.g. in the file explorer. A java source file isn't a (click-launchable) executable, and - without some acrobatics - neither is a compiled .class file: you shouldn't expect to double-click either and start your program.
In order to get this sort of behavior, you'll need to build a launchable program, and there are a few ways to do this. One is by making a batch file that runs the java VM with your code, another is creating an executable jar file.
To just run your code outside your IDE, you can invoke the java VM on the command line:
c:\> java HelloWorld
As to your specific error message, you haven't associated any program with .java files. Typically, as programmers, we want this Windows file association to be our editor of choice or our IDE. You can create this association by Right-clicking on the file, choosing Properties from the menu and then clicking the Change button beside Opens with: to pick an application.
But this is a side-issue: you still won't use this to make .java file executable. Search around this site for questions and answers about building executable jar files. If you're using a specific IDE like Eclipse or NetBeans, use that to refine your search.
When you want to execute the program, you specifiy the class name rather than the file name:
java HelloWorld
Don't use java HelloWorld.java, for instance.
Related
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.
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.
I'm currently in the process of learning more about Java. I downloaded a sample program that has about 5 different .java files in the source folder.
Whenever I try to execute the class files in the command prompt using "java ClassNameHere", I get a message in the cmd prompt that says "Error: Could not find or load main class ClassNameHere".
I've tried recompiling them using the javac command and they compiles without any issues. Its just the class file i'm having trouble with.
In addition to this, I also cannot run the java files using Eclipse. Instead of getting an option to "Run as Java Application", "non applicable" appears under the "Run as" tab in Eclipse.
I've tried a number of different solutions already posted online, including another command that uses "java -cp . ClassNameHere" or something along those lines and they have not worked for me. Any help is appreciated.
Edit
Link to program from my drive since I'm not home
https://docs.google.com/file/d/0B9_pcTVZTnfEeVdzZmM0ZmFkTmc/edit?usp=docslist_api
Here is a a procedure that will get the application working in Eclipse. This procedure assumes the following:
Windows 8. I don't think Linux/Mac OS X procedure would differ greatly, but just in case...
In Eclipse:
Right click in Package Explorer, and select New --> Java Project.
In the New Java Project dialog, uncheck Use default location, and click Browse and select the directory you've extracted the zip to, or type the path in the box.
Click Finish.
From this point, you can run this is a Java Application from Eclipse.
Confirm that at least one of the classes has a main() method. That would be required for Eclipse to show "Run as Java Application".
Follow this steps:
Run Command Prompt.
Use cd C:\path\to\your\porject\java classes.
Use set path=%path%;C:\Program Files\Java\jdk1.7.0_65\bin (CHANGE THE PATH TO YOUR jdk1.x.x_xx\bin FOLDER) to tell the system where to find jdk programs.
Use javac MainClass.java to compile the project, MainClass.java si the java class where the main method is.
Use java MainClass (WITHOUT .java EXTENSION) to run the project.
After the last command your project should run correctly.
Sorry for the "use" ripetition but i'm not english.
I encountered this problem several times. If I pack a Java application in an executable jar file that takes arguments from a user, then the user have to invoke the program (jar file) from the command prompt by the following command:
java -jar "jar-file-name.jar"
But I want that whenever a user double clicks on the executable jar file (that needs arguments from the user), a window (command-prompt window) appears that would appear if we had invoked the jar file from the command-prompt.
I know one solution to this is using batch file .bat to run the jar file. Is there any other solution?
To explain why:
There are 2 java JVM exe launchers:
java.exe: console based - provides console input/output.
javaw.exe: for GUI apps - hides the console.
JAR file extensions are associated with javaw.exe by default, which is why you don't get a console when you double-click them.
The answers others have given, and adding my own:
rewrite your app so that it uses Java GUI items for input and output instead of System.in/System.out. This may be over-complex for what you require.
You mentioned creating a batch file so that the console-based Java JVM (java.exe) is run, You could also create a windows shortcut specifying the command line: java -jar jar-file-name.jar
You could change the windows file associations for .jar (but generally this is a bad idea -- new Java installs may reset this, and it will mean all java apps run from jars will have a console)
You could use a Java launcher like WinRun4J which allows you to simply drop a double-clickable EXE with an icon and a config file that specifies how your app should be run (with/without console, and with any other JVM and command line parameters )
Personally I went for the last option in my project - I made my jar file non-executable, and the user has to double-click the EXE. It also allowed me to specify a nice icon for my project, and provide multiple options on launch (debug/non-debug mode) simply by having a different exe/config file.
You would need to reassign the explorer association for ".jar" to java.exe instead of javaw.exe. This is somewhat of a questionable thing to do - it might make more sense to create your console output window. You can, of course, trivially pop up a dialog for user input of required execution parameters if they are not supplied on the command line.
There is working code for a console output JTextArea here.
This is an operating system feature.
If you want full control, then you must provide this functionality inside your program (or with a wrapper class.
Without learning new programing languages, can we using Java get .exe (executable windows file) software directly? And is there away to make .jar (Java ARchive) software transform to.exe (executable windows file)?
Would this conversion effect the performance of the software?
One of the important points of Java is that it'll run on any platform (e.g. Windows, Linux, etc) that has a suitable Java Virtual Machine (JVM) installed. A .exe file is compiled to work on only one platform.
What you think you want is to turn a .jar into an .exe so you can launch it easily. What I expect you really want is just an easy way of launching a Java app that anybody can understand, including your parents.
The easiest way of doing this is to create a Windows batch file that launches your Java app. One of line of script, one new file, one new instruction to your users - "Double click on runme.bat"
There are ways of turning a .jar into an .exe, but you should think about whether that's really what you want, and if so, why.
Note: you can launch a .jar in Windows by just double clicking on it, as long as the main class is specified in the manifest. You might want to look into tools like Apache Ant to simplify building .jars and their manifests. Just a thought.
EDIT:
A batch file in Windows is a simple text file that contains commands. You can test the commands by running them in the command prompt (Start -> Run -> cmd). When you run a batch file the commands in it are just fed to the command prompt one at a time.
How to run a Jar from the command prompt: "java -jar myfile.jar"
If you create a batch file (.bat - use Notepad or your favourite text editor) containing "java -jar myfile.jar" (without the quotes!) then double-clicking it will launch the main class specified in the manifest of myfile.jar. myfile.jar has to be in the same folder as the batch file, though.
If you use the command "java -jar lib\myfile.jar" (again, without the quotes) then your batch file will run myfile.jar, which needs to be in a folder called lib that's in the same folder as the batch file. Use this approach when you have a whole load of Jars with your application and you don't want to shove them in the user's face :)
Note that moving the batch file will break it, unless it uses absolute paths to the jar file - e.g. "java -jar C:\dev\myfile.jar". Of course, if you use absolute paths then moving the Jar will break the batch file anyway :)
Also, note that you should be able to run a Jar file just by doubling clicking on it in Windows, as long as the main class is specified in the manifest. Give it a try. Of course, convincing your users that they can double click on it is another matter entirely...
As a final note, if you use a batch file, your users will get a nice command prompt window sitting in the background until your Java app closes. That is, unless you start your batch file command with "start". E.g. "start java -jar myfile.jar". If you have your app configured to log to System.out or System.err, you will still get a command prompt when your app writes to either of those streams, though.
Final final note, in the Linux world the equivalent of batch files are shell scripts.
You can launch a .exe from java with Runtime.exec() or ProcessBuilder.
You can make a .exe launcher with http://launch4j.sourceforge.net/
Yes, it is possible: http://www.excelsior-usa.com/articles/java-to-exe.html
I'm not sure if this is an appropriate answer because I don't work with Java but could you not using a shortcut file pointing to the java runtime and pass the jar as an argument, this
I realise this is not the only way, nor probably the best way but to me it seams a little better than creating a batch or converting to an exe file if all you're looking for is a convienient way to launch a java app
The very simplest way is to write a windows batch (.bat) file which will start the application.
I was using a wrapper for java to make exe files for a pretty long time:
JStart32
It is just a wrapper for *.jar files.
But ask yourself:
Wouldn't an exe kill the purpose of javas platform independency?
Check out jsmooth (free) and exe4j and you might want to read make your swing app go native