1

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.

4
  • 3
    What command are you trying to use to execute it? On a different note, you should be executing HelloWorld.class, the compiled file, not the source Commented Oct 2, 2012 at 21:18
  • 2
    When executing, your program should not care about the HelloWorld.java source file. It should only care about the .class file or .jar file it was compiled into. Commented Oct 2, 2012 at 21:19
  • 3
    .java files are Java source files. Read the Getting started Java tutorial: docs.oracle.com/javase/tutorial/getStarted/index.html Commented Oct 2, 2012 at 21:20
  • I was trying to compile a program from the command line and I still have not been able to get it to work. I set the path for the compiler which on my machine at work is C:\Program Files\Java\jre6\bin and then use javac --> give error java --> said that it found the HelloWorld.java file but there was not main. But there is a main class because I looked and it was there. What else might be wrong? Commented Feb 3, 2014 at 19:19

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well double-clicking a .java file should open it for editing via notepad... However, yes, that's probably what happened with the .class file
@AlexColeman OP is trying to open HelloWorld.java , so it is definitely java file :)
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.