I made this tiny code:

public class main
{
	public static void main(String[] argv)
	{
		System.out.println("Hey");
	}
	
}

It compiles and run fine in eclipse, but when i go into cmd and do
javac main.java it created a main.class
then I did
java main.class
but I get errors..

C:\ecjava\herp\src>java main.class
Exception in thread "main" java.lang.NoClassDefFoundError: main/class
Caused by: java.lang.ClassNotFoundException: main.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: main.class. Program will exit.

How can I fix it?

I also want to make an executable JAR, making it easy to just send it to a friend who can run it.
But I believe it produced the same error as above.

java,exe takes the name of your class, not the name of your class file...
so that should be simply
java main

java,exe takes the name of your class, not the name of your class file...
so that should be simply
java main

Weird, I think I did *.class before...

Anyway

New error:

C:\ecjava\herp\src>java main
Exception in thread "main" java.lang.UnsupportedClassVersionError: main : Unsupp
orted major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: main. Program will exit.

That's weird. That error normally means the code was compiled for a particular target version of the JRE (eg 1.6), but the actual JRE you are using is an earlier version (eg 1.4.2). But if you have done a proper install of a current JDK on your machine then the compiler and JRE versions should match. Anyway, check the compiler and JRE versions, and reinstall with the latest & greatest from Oracle (1.7) if in doubt.

That's weird. That error normally means the code was compiled for a particular target version of the JRE (eg 1.6), but the actual JRE you are using is an earlier version (eg 1.4.2). But if you have done a proper install of a current JDK on your machine then the compiler and JRE versions should match. Anyway, check the compiler and JRE versions, and reinstall with the latest & greatest from Oracle (1.7) if in doubt.

Thanks a lot.
I reinstalled the JDK and it runs fine now.

How do I create an executable JAR that I can simply send to a friend?
I'm going to try some stuff, and check back here to see if someone has replied.
Will write if I make it work.

I tried this, got an error (of course...)

C:\ecjava\herp\src>dir
Volymen i enhet C har ingen etikett.
Volymens serienummer är FE75-353C

Innehåll i katalogen C:\ecjava\herp\src

2011-09-06 18:16 <KAT> .
2011-09-06 18:16 <KAT> ..
2011-09-06 18:27 407 mainx.class
2011-09-06 18:16 110 mainx.java
2 fil(er) 517 byte
2 katalog(er) 106 400 657 408 byte ledigt

C:\ecjava\herp\src>jar cvfm Run_Me.JAR mainx.class
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:406)
at java.util.jar.Manifest.read(Manifest.java:199)
at java.util.jar.Manifest.<init>(Manifest.java:69)
at sun.tools.jar.Main.run(Main.java:172)
at sun.tools.jar.Main.main(Main.java:1177)

Uh, ok..

I managed to do it:

C:\ecjava\herp\src>echo Main-Class: mainx.class > manifest.txt

C:\ecjava\herp\src>jar cvfm Run_Me.JAR manifest.txt mainx.class
tillagt manifestfil
lõgger till: mainx.class(in = 407) (ut = 278)(31% packat)

I now have a .jar file, but how do I run it.......?
When i try to open it, my computer doesn't know what to open it with.

*sighs* ok...
I located java.exe inside my JRE folder, it seemed to run, but yet again generated an error.

Error: Could not find or could not load the main class: C:\ecjava\herp\src\Run_Me.JAR

java -jar myjar - sounds like you may have missed the -jar???
(Going offline now - back tomorrow. Good luck)

Uhm....
I have the .jar file,
doing "java -jar Run_Me.jar" works, but is there not a way to make it run by just double clicking the jar?

way to make it run by just double clicking the jar

Most java installations on Windows add an entry to the OS's file association table to open a jar file with the correct command line so when you double click on the the jar file the command is executed with the commandline: java -jar <JARFILE>.jar

What version of Windows are you on? How did you install java?

Most java installations on Windows add an entry to the OS's file association table to open a jar file with the correct command line so when you double click on the the jar file the command is executed with the commandline: java -jar <JARFILE>.jar

What version of Windows are you on? How did you install java?

Windows 7.
How I installed java?
I don't understand. I just downloaded the JDK and installed it.

When you look at the files in the folder with a jar file using Windows Explorer, what is displayed in the Type column for a jar file. On my system (XP) its: Executable Jar File
That name was set when I installed the JDK.

What happens when you double click on a jar file?

When you look at the files in the folder with a jar file using Windows Explorer, what is displayed in the Type column for a jar file. On my system (XP) its: Executable Jar File
That name was set when I installed the JDK.

What happens when you double click on a jar file?

It just says JAR-file.

If i doubleclick it, it just pops up a cmd window for <1 second saying Error: Could not find or could not load the main class C:\ecjava\herp\src\bin\Run_Me.jar

That error looks like what you'd get if you left off the -jar option with the java command.

Do you know how to add entries to the OS's file association table? Here are instructions on how to see what is in the table for the jar extension. I have no idea how to do this in Windows 7.

Open Windows Explorer
Click on Tools menu
Click on Folder Options
Select File Types tab
Scroll down in the "Registered file types:" list until you see an entry for JAR.
Select the JAR entry.
Click the Advanced button
In the Edit File Type window scroll down the list of Actions until you see the one in bold.
Select the bold one (The default)
Click the Edit button
Select, Copy and paste here what is in the "Application used to perform action:" window.

Be careful not to change anything and Cancel your way out of the open windows.

That error looks like what you'd get if you left off the -jar option with the java command.

Do you know how to add entries to the OS's file association table? Here are instructions on how to see what is in the table for the jar extension. I have no idea how to do this in Windows 7.

Open Windows Explorer
Click on Tools menu
Click on Folder Options
Select File Types tab
Scroll down in the "Registered file types:" list until you see an entry for JAR.
Select the JAR entry.
Click the Advanced button
In the Edit File Type window scroll down the list of Actions until you see the one in bold.
Select the bold one (The default)
Click the Edit button
Select, Copy and paste here what is in the "Application used to perform action:" window.

Be careful not to change anything and Cancel your way out of the open windows.

It's Java Platform SE Binary

What is the "it" in "It's Java Platform SE Binary"?

What is the "it" in "It's Java Platform SE Binary"?

the application used to open the .jar

Those are words to describe something, they are not a commandline with starting with an exe file.
Here's what I find for the commandline to execute a .jar file when it is double clicked:
D:\Java\jre6_10\bin\javaw.exe -Xmx256M -jar "%1"

Those are words to describe something, they are not a commandline with starting with an exe file.
Here's what I find for the commandline to execute a .jar file when it is double clicked:
D:\Java\jre6_10\bin\javaw.exe -Xmx256M -jar "%1"

Googled and saw somewhere " ..javaw.exe -jar "%1" "
But I don't understand where I need to write that..

I can run the .jar by doing "java -jar myJarFile.jar"
but I want it to run by a simple double-click.

You need to tell the OS what commandline to use when a file is double clicked. I explained how to look at your current settings a few posts back. What happened when you tried to do what I suggested?

Open Windows Explorer
Click on Tools menu
Click on Folder Options
Select File Types tab
Scroll down in the "Registered file types:" list until you see an entry for JAR.
Select the JAR entry.
Click the Advanced button
In the Edit File Type window scroll down the list of Actions until you see the one in bold.
Select the bold one (The default)
Click the Edit button
Select, Copy and paste here what is in the "Application used to perform action:" window.

Be careful not to change anything and Cancel your way out of the open windows.

You need to tell the OS what commandline to use when a file is double clicked. I explained how to look at your current settings a few posts back. What happened when you tried to do what I suggested?

Open Windows Explorer
Click on Tools menu
Click on Folder Options
Select File Types tab
Scroll down in the "Registered file types:" list until you see an entry for JAR.
Select the JAR entry.
Click the Advanced button
In the Edit File Type window scroll down the list of Actions until you see the one in bold.
Select the bold one (The default)
Click the Edit button
Select, Copy and paste here what is in the "Application used to perform action:" window.

Be careful not to change anything and Cancel your way out of the open windows.

I was unable to follow your directions as I'm using Windows 7.
I tried searching Google but found nothing of use.

Ahh yes, MS has improved their OS again.
Are you able to execute the RegEdit program?
Do you want to manually change the registry to add the commandline you want?
Perhaps someone could send you a copy of their registry entry that you could modify and merge into your Registry to give you what you need.

Ahh yes, MS has improved their OS again.
Are you able to execute the RegEdit program?
Do you want to manually change the registry to add the commandline you want?
Perhaps someone could send you a copy of their registry entry that you could modify and merge into your Registry to give you what you need.

I downloaded "Default Programs Editor"
in open with i have
"C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar"%1"
But when I duoble click a .jar now, it will say Couldn't creat the virtual machine.
Program will exit.

Is there a space between -jar and "%1" ?

Oh look! It worked!
I needed a space between -jar and "%1"

Excellent! Is this thread "solved" now?

"Default Programs Editor"

Do you have a link for the program you downloaded?

Thanks. I'm getting a Windows 7 system soon and have many custom Rightclick context menu items I want to have available.

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.