问题
So I've written a funny little program and I want to show it to some of my friends. My friends, not being programmers, have no idea what to do if I send them the folder containing the necessary classes and files. I want to be able to email them something (or put it on a cd/thumbdrive) that they can then double click and have it run the program. I have absolutely no clue how to make this happen. I'm taking a class and we use linux computers (I use a mac when I'm not in class) and we have to javac the .java files and then java "File name" to make it run. I have friends on Mac's and PC's and I want them to be able to just click the program and have it go....
If it makes a difference the program is written using the object draw library.
回答1:
Use the jar
command to build an executable jar file. It's basically a ZIP file with certain guarantees.
Within the jar file you will need (it's a reqirement) a /META-INF directory with a file in it called the manifest (/META-INF/MANIFEST.MF). It describes the "manifest" of the jar file, which is in some ways modeled off a shipping manifest.
Inside the MANIFEST.MF file, you need the directive
Main-Class: org.mystuff.path.Main
Which should direct the JVM to run the org.mystuff.path.Main class when the jar file is "exectued" via the command
java -jar myproject.jar
Note that JAR files tend to handle classpaths differently, in the sense that they ignore the CLASSPATH environmental variable, and the -classpath command line options. You need to add a classpath directive within the MANIFEST.MF file if you need to reference other JAR files. Use the manifest link above to see the details concerning embedding a classpath.
Depending on the simplicity of the "project", a single JAR file might be enough to ship to them; however, if you need more than that, you might find yourself having to ship a few files in a few directories. In that case, put the files (including the JAR file) and directories in a second zip file. While you could select from a number of different ways to "package" the items, I recommend
(layout inside the zip file)
/Readme.txt (a text file describing what to do for new comers)
/License.txt (a text file describing how liberal / restrictive you wish to be concerning your authorship rights.
(the license sounds like overkill, but without it nobody can prove they're not breaking the law)
/bin/myprogram.sh (shell script containing "java -jar ../lib/myprogram.jar")
/bin/myprogram.cmd (Windows batch file containing "java -jar ..\lib\myprogram.jar")
/lib/myprogram.jar (the executable jar file containing your compiled code)
/lib/otherjar.jar (other jar files, as necessary)
With such a zip-file structure, the installation instructions then become "unzip the zip file; change directory to "bin" and run "myprogram.whatever".
回答2:
You need to tell us which IDE you are using, but just compile it into an executable jar, they work on all systems that have the java version you compile them again (so multiplatform)
回答3:
I would say use an applet then there is no need for any commands to run. Here is what you can do
- If you have multiple classes then create a executable jar, If don't want to create jar move all your code into one class
- Then create a class that extends Applet, You can load your program from this applet class or even better move every thing to the applet class (not the best design, but makes your life easier in this case)
Then create a html file some thing like this, This html has script that will direct what to do to view the program
<script> var jEnabled = navigator.javaEnabled(); if (!jEnabled){ document.write("<a href=\"http://www.java.com/en/download/help/enable_browser.xml\">Enable Java</a> on your browser to view me "); } </script> <br><br> <applet code="AppletLauncher.class" width=100 height=140/>
回答4:
Using objectdraw to create an executable jar file:
Typically, objectdraw is used with the BlueJ IDE so these instructions are for using BlueJ and the objectdraw library.
Make sure you have a Class that has the begin() method. This is the class that you run inside BlueJ and now you want to turn it into an executable jar file.
You need to make a new Class, let's call it Main. It should look like this:
public class Main{ public static void main(String[] args){ ClassWithABeginMethod game = new ClassWithABeingMethod(); game.begin(); } }
Now, in BlueJ, open the Project dropdown menu. Select "Create Jar File...".
In the dialogue box that opens: Make sure that the Class "Main" is the one selected for the Main class option. Under "Include user libraries", select the "objectdraw.jar" option. Press continue.
The new dialogue box that opens is asking where you want to save a new folder that will contain your new jar file. Name it what you would like. The result will be a new folder with the name you selected that contains a jar file of the same name.
You're done. Use the command line prompt: "java -jar jarFileName.jar" to run your new jar file.
来源:https://stackoverflow.com/questions/7533433/how-do-i-make-my-program-written-in-java-usable-to-other-people