I have just started learning java and made an first mathematical interactive program which I wanted to distribute with my friends . But I am unable to run the .jar file .
I do the following , I click on export - Runnable.jar - Main.class and select the other options and save it . But when I run it , nothing happens :( .
I have tried looking around and I found some answers but they weren't helpful .
So how should I make an executable.jar with eclipse or without eclipse ?

5
Contributors
45
Replies
46
Views
5 Years
Discussion Span
Last Post by im abcd
Featured Replies
• 1

Certainly the obvious advice is to update your program to use a GUI. This is 2012, and console-based end-user applications just aren't written any more (except in the early stages of learning Java). You could distribute it with a tiny batch file that just has the single line java -jar …

might be because you haven't got your environmental properties in your OS set right. if that's the case, you can click all you want. as long as the os doesn't know where your jre and libs are, it'll be treated as a simple archive, not an executable.

And so how to set the environmental properties right ?

depends on what OS you're using.

I am using windows 7 ultimate .

I tried that , restarted the pc and then tried to export but to no avail . It doesn't runs .
Do I have to add anything to my code ?

My source:

// Apollonius theorem.
// All credits go to 'im abcd'
import java.util.Scanner;
class Main {

public static void main(String args[]){

Scanner abcd = new Scanner(System.in);
int reply , Side1_Side2 , HalfSide3 , median , Side3;
int Side;
int ans;

int free ;

System.out.println("Press 1. if Side3 is to be found ");
System.out.println("Press 2. if Median is to be found and Side 3 is given .");
System.out.println("Press 3. if Median is to be found and halfSide3 is given.");
System.out.println("Press 4. if Side 3 is to be found and median is given : ");
System.out.println("Press 5. if Side 1 or Side2 are not given and all other values are given :");

System.out.println("Enter Side1*Side1+Side2*Side2 : ");
Side1_Side2 = abcd.nextInt();

System.out.println("Enter the given median : ");
median = abcd.nextInt();
System.out.println("To find Side3 , we must find find HalfSide3*HalfSide3");
System.out.println("The formula for finding HalfSide3 is : 2(Side3) = Side1*Side1 + Side2*Side2 - 2(median*median) ");

ans = (Side1_Side2) - (2*median*median) ;
System.out.print("2(HalfSide3*HalfSide3)=" );
System.out.println(ans);
System.out.print("Therefore , Half Side 3 * Half Side 3 = " );
System.out.println(ans / 2);
HalfSide3 = ans / 2 ;
System.out.print("Half Side 3 = ");
System.out.println(Math.sqrt(HalfSide3));
Side3 = (int) Math.sqrt(HalfSide3);
System.out.println("Therefore : Side 3  = HalfSide3+HalfSide3");
System.out.println((Side3) + (Side3));
}

System.out.println("Enter Side1*Side1+Side2*Side2 : ");
Side1_Side2 = abcd.nextInt();

System.out.println("Enter Side3 ");
Side3 = abcd.nextInt();
System.out.println("Therefore , HalfSide3 = Side3/2");
HalfSide3 = Side3 / 2 ;
System.out.println("To find the median , we must first find 2(median*median)");
System.out.println("Therefore ,Formula for finding 2(median*median) is : Side1*Side1+Side2*Side2 - 2(HalfSide3*HalfSide3)");
System.out.print("Therefore , 2(Median*Median) = ");
System.out.println((Side1_Side2) - (2*HalfSide3*HalfSide3));
ans = (Side1_Side2) - (2*HalfSide3*HalfSide3);
System.out.print("Therefore  , median*median = ");
System.out.println( ans / 2);
System.out.print("Therefore median   = ");
median = ans / 2 ;
System.out.println(Math.sqrt(median));

}

System.out.println("Enter Side1*Side1+Side2*Side2 : ");
Side1_Side2 = abcd.nextInt();

System.out.println("Enter halfSide 3 : ");
HalfSide3 = abcd.nextInt();
System.out.println("To find the median , we must first find 2(median*median)");
System.out.println("Therefore ,Formula for finding 2(median*median) is : Side1*Side1+Side2*Side2 - 2(HalfSide3*HalfSide3)");
System.out.print("2(median*median = )");
System.out.println((Side1_Side2) - (2*HalfSide3*HalfSide3));
ans = (Side1_Side2) - (2*HalfSide3*HalfSide3);
System.out.print("Therefore  , median*median = ");
System.out.println( ans / 2);
System.out.print("Therefore median   = ");
median = ans / 2 ;
System.out.println(Math.sqrt(median));
}

System.out.println("Enter Side1*Side1+Side2*Side2 : ");
Side1_Side2 = abcd.nextInt();

System.out.println("Enter the median  : ");

median = abcd.nextInt();

System.out.println("To find side3 we must find halfside3 . ");
System.out.println("Therefore , 2(HalfSide3*HalfSide3) = Side1*Side1+Side2*Side2 - 2(median * median)");
System.out.print("Therefore , 2(HalfSide3 * HalfSide3) = ");

System.out.println((Side1_Side2) - (2*median*median));
ans = (Side1_Side2) - (2*median*median);

System.out.print("Therefore , HalfSide*HalfSide3 = ");
System.out.println(ans  / 2);
free = ans / 2 ;
System.out.print("Therefore , HalfSide3 = ");
System.out.println(Math.sqrt(free));
Side3 = (int)Math.sqrt(free);
System.out.print("Therefore Side3 = ");
System.out.println(Side3 + Side3);

}
System.out.println("Enter Side1 or Side2(Whichever is given) :");
Side = abcd.nextInt();
System.out.println("Enter the given median : ");
median = abcd.nextInt();
System.out.println("Enter HalfSide3 ");
HalfSide3 =  abcd.nextInt();

System.out.println("To find the remaining side , the formula is : Side2*Side2 = Side1*Side1 - 2(Median*Median) + 2(HalfSide3*HalfSide3)");
System.out.print(" Therefore Side2*Side2   = ");
System.out.println((Side*Side) - ((2*median*median)+ (2*HalfSide3*HalfSide3)));
ans = (Side*Side) - ((2*median*median)+ (2*HalfSide3*HalfSide3));

System.out.print("Therefore , Side2 = ");
System.out.println(Math.sqrt(ans));

}

else{
System.out.println("Error: You entered an invalid number");

}

}

}


Thanks .

If you have the default installation then your jar is being run by javaw.exe, not java.exe
That means there is no console window, so you don't see any IO from a program that just uses the console.
Try running the jar with java.exe in a command window.

Edited by JamesCherrill

Umm , I am not sure but we run it by typing : java -jar yourfile.jar , right ?
When I tried that it said : Unable to access jarfile Apollonius.jar

Also when I create a jar file using eclipse , a folder called MyFilename_lib gets created but its empty .

Without peering over your shoulder I can't see why/where you would get that folder.
Can you see the jar file?
Open the jar file with a zip utility (any one will do). a Jar is just a zip file, so you will be able to see its contents. You should be able to see your class file(s) and a folder META-INF with a file called "manifest"

Yup , I can see my class named " Main.class " And MANIFEST .

In that case I can see no reason wy java -jar Apollonius.jar will not work, assuming that java.exe is in the path, and the jar file is in the current directory.
Anybody else got any ideas???

What version of JDK/Eclipse are you running?

Edited by Starstreak

Yes it points towards the class with the main method and I viewed that thread , it didn't help :(
Would anyone like to help me via team viewer ?

Add me on im_abcd@hotmail.co.in .

Edited by im abcd

what happens if you type
dir Apollonius.jar
at the same command promnpt?

On windows:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

This :

19-09-2012  15:26             2,204 Apollonius_Theorem.jar
1 File(s)          2,204 bytes
0 Dir(s)  26,901,270,528 bytes free


Edited by im abcd

"Unable to access jarfile Apollonius.jar"

but that's not the correct file name! Its Apollonius_Theorem.jar

you must use the correct jar file name in the java command.

My bad , but its the same with Apollonius_Theorem.jar

Error: Unable to access jarfile Apollonius_Theorem.jar

Please copy and paste here the full contents of the command prompt window so we can see exactly what is happening. Do a dir command in addition to the java command you are using.

Edited by NormR1

Omg , it was my mistake after all .
When I use to use "

java -jar Apollonius_Theorem


I didn't use to put " .jar " after apollonius_theorem .
So now after puting it , it works fine .
But it only works from cmd , how do I make it run directly by double clicking ?

by setting your path and classpath in your environment settings of your os correctly.

how do I make it run directly by double clicking ?

There needs to be an entry in the Windows Registry that specifies the command line to use when a .jar file is "opened" with a double click. Normally installation of the JRE sets the value.

There don't need to be an environment variables set.

Hmm, I searched over the internet and came over this :

Open a command prompt window using "Run as administrator”, and then type: -
C:\>assoc .jar=jarfile
C:\>ftype jarfile="C:\Program Files\Java\jre\bin\javaw.exe" -jar "%1" %*
C:\>ftype jarfile="C:\path\to\your\javaw.exe" -jar "%1" %*


After that I tried running it and it asked for which program should I run it with . I browesed upto my java folder and selected java.exe . Now, when I double click the program starts and instantly closes . A error , can't find or open the main class comes up in the window which opens and closes instantly .

it asked for which program should I run it with

If the OS built the Registry entry from that prompt, it probably left out the -jar option.
Look at using the regedit program to look at the registry to see what is there. Be very careful not to change anything in the registry.

What should I look for in the registry , any key , string name ?

Look for .jar (it should have data = jarfile)
then look for jarfile (it should have a command line)

Edited by NormR1

The value data is : jarfile .
The binary data is :

0000 6A 00 61 00 72 00 66 00 j.a.r.f.
0008 69 00 6C 00 65 00 00 00 i.l.e...
0010


So what's wrong in these values ?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.