1

Apologies for the generic nature of this question.

I have been learning Java for a few months now, and I've created a few simple projects which have some functionality. I wanted to send the projects to a friend and I'm running into countless errors and struggles. Passing a .jar file is causing "Main class not found" errors when they try to open it.

I tried using third party software to wrap the .jar files into an .exe file and the same errors still persist.

Beyond that, I'm convinced that passing around .jar files wrapped into .exe files via third party software is NOT how Java was intended to be used. I've read two books on Java and they all talk about structuring the language, but I'm confused about WHERE I'm supposed to be using this code because it has become painfully obvious that it is NOT intended to be passed around in file format.

Is this a server programming language? Used on the back end of websites mainly? I'm not sure where one would be using the code written in Java.

leigero
  • 3,233
  • 12
  • 42
  • 63
  • *"Beyond that, I'm convinced that passing around .jar files wrapped into .exe files via third party software is NOT how Java was intended to be used."* Good observation. I recommend [Java Web Start](http://stackoverflow.com/tags/java-web-start/info) instead, though those `NoClassDefFoundErrors` will need to be sorted first. I suggest you ask a question on that. Voting to close this one as off-topic. – Andrew Thompson Dec 03 '12 at 04:54
  • 1
    Yes, Java code often runs on a server. However, you can also write standalone java programs that can be executed as jar files. The key is making sure the jar is packaged properly. If you're writing programs with Java 7, don't expect any of your buddies to be able to use them. – jahroy Dec 03 '12 at 04:54
  • possible duplicate of [How do I create executable Java program?](http://stackoverflow.com/questions/804466/how-do-i-create-executable-java-program) – Abdullah Jibaly Dec 03 '12 at 04:56
  • @jahroy The `deployJava.js` (the deployment toolkit scrpt) can help ensure the end user has the right minimum Java to run a JWS based application, or an applet. Technically, it could also be used to link directly to an executable Jar as well, though the end user would be well advised not to run a 'no security manager' executable Jar from a random link on the net. ;) – Andrew Thompson Dec 03 '12 at 05:00
  • possible duplicate of [How to make an executable jar file?](http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file) – Stephen C Dec 03 '12 at 05:05

5 Answers5

4

You should build an executable jar.

Check here and here.

Community
  • 1
  • 1
Azodious
  • 13,752
  • 1
  • 36
  • 71
2

You need to create your JAR file as an executable JAR file if you want someone to be able to run it. That is how you send around Java executables. Look here for more info:

How do I create executable Java program?

Community
  • 1
  • 1
Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
1

I'd recommend you spend some time with http://docs.oracle.com/javase/tutorial/. Java program basically runs on top of virtual machine (not directly on your physical machine). Java program is compiled into *.class files, and a collection of *.class is commonly bundled into a *.jar file.

If you want to run your java program, one common way is to execute the java virtual machine runtime (JRE) and specify your jar package to be loaded into the classpath, eg:

java -jar /path/to/my.jar

Your jar file has to be packaged properly such that it indicates what is the main class (if any)

Packaging jar into exe is possible, but is not best practice. Java paradigm is a write & compile once -- runs everywhere.

gerrytan
  • 40,313
  • 9
  • 84
  • 99
1

from Oracle:

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

Main-Class: classname The value classname is the name of the class that is your application's entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args).

After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

java -jar JAR-name The main method of the class specified in the Main-Class header is executed.

Setting an Entry Point with the JAR Tool:

The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file. For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:

jar cfe app.jar MyApp MyApp.class You can directly invoke this application by running the following command:

java -jar app.jar If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

jar cfe Main.jar foo.Main foo/Main.class

Setting an Application's Entry Point

vishal_aim
  • 7,636
  • 1
  • 20
  • 23
0

For a quick answer, libraries are bundled in jars, which are then added to the classpath of another application.

Pretend you invent a new sort algorithm which is faster than all the others. You could bundle up a small number of classes into a jar, which is basically just a zip file containing your compiled .class files.

Now your friend needs to sort some data and wants to use your classes. He would write his code to import your classes (using import at the top of his .java files), and then work with them in his code. When it came time for him to compile his .java files into .class files, he would add your jar to his classpath. Eclipse/netbeans can set this up for you, or if you're running javac from the command line it would look something like this:

javac ... -cp "fastsorting.jar" ...

Sometimes (rarely, in the real world) someone has a JAR which is really a fully fledged program, meant to be run. I say rarely, because jars aren't the most common way to distribute software. More popular is as a web service or through an applet on a website. In this case, a jar's manifest file (just a text file telling java information about this jar) will have a main class, which is run when the the jar is invoked through java.

Cory Kendall
  • 7,195
  • 8
  • 37
  • 64