Distributing an executable Jar console app - java

(Using JDK7)
A friend of mine asked me to write him a program for rolling dice of arbitrary number and size. So I did. For simplicity's sake, I kept it as a console app. Now I'm trying to distribute it to him. I tried exporting it as a Runnable Jar with Eclipse's built-in exporter.
After opening it with Java (As opposed to javaw)... Nothing happens. I'm able to take a screenshot of the command prompt before it closes, and it reads:
Error: Could not find or load main class C:\Users\Matt\Desktop\Roller.jar
Contents of Roller.jar
META-INF/MANIFEST.MF
roller/Roller.class
Contents of MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: .
Main-Class: roller.Roller
Contents of Roller.java:
public class Roller{
/* Irrelevant code expunged */
public static void main(String[] args){
//Irrelevant code expunged
}
}
I want to get this in a state where I can just email him the .jar and he can double-click it to run it. Can anyone help?

In Roller.java, you need to add package roller; to the top of your class to match Roller.class being in the roller folder in the JAR file. After this, you don't even need to specify a Class-Path in your Manifest file.

Related

Can't find or load main class, in a .jar file

Been looking for around 2 hours for a solution to my problem, tried countless solutions and still the problem remains unsolved.
I have a game project with few packages; geometry, levels, decoration, and game. Inside the game package resides a gameRun.java
package game;
public class gameRun {
/**
* Program entry point.
* #param args the arguments for the levels
*/
public static void main(String args[]) {
... some code
}
}
and I have the following manifest file :
Main-Class: game.gameRun
Class-Path: someExternalGraphicTool.jar
//newline here
I compile my whole game using the following command : (the bin folder already exists).
javac -d bin src/geometry/*.java src/decoration/*.java src/levels/*.java src/game/*.java
I then turn my compiled classes into an executable .jar file using the command :
jar cvfm t.jar manifest.mf bin/geometry/*.class bin/decoration/*.class bin/levels/*.class bin/game/*.class
I then try to run my t.jar using :
java -jar t.jar
//or
java -cp .:t.jar game.gameRun
which both produce the error, "Error: Could not find or load main class game.gameRun"
However, when I unzip the jar file, inside the bin/game directory I can see a gameRun.class file and inside the META-INF\MANIFEST.MF file I can still see that the Main-Class is set to game.gameRun.
Note that I am working on a remote linux server, on a command line, and the jar file doesn't work even if I download it to my windows 10 machine.
What did I do wrong during this process ? Thanks for any help.
Your classes are in a wrong structure inside a jar file, because of the bin folder. My suggestion: pack it all into a jar starting in a bin folder.
Now when you extract your jar you will see 2 folders: META-INF and bin.
If you make the jar from bin folder you will see: META-INF and game, and it will work.
It doesn't work for you simply, because it can't find the main class, since it is inside the bin/game/YourClass.class, not in game/YourClass.class.
...\bin> jar cvfm t.jar manifest.mf geometry/*.class decoration/*.class levels/*.class game/*.class
And then just:
...\bin> java -jar t.jar

Command line java error: Could not find or load main class

I'm trying to run a project from the command line (Ubuntu 14.04). The main class is called Demo, and I have a Demo.java, a Demo.class, and a Demo$1.class all in the same directory (I know it would be better to seperate them).
I wrote my own Manifest file, MANIFEST.MF, which just looks like this:
Main-Class:Demo
I made sure to end it with a newline.
Next, I want to create my .jar file. I did so with this command:
jar -cfm example.jar MANIFEST.MF *.class
Then, I try to run my project like so:
java -Djava.library.path=/path/to/dependencies -jar example.jar
I seem to get the following error no matter what I try:
Error: Could not find or load main class Demo
I've actually never compiled/run a Java project from the command line before, it's possible I'm making a stupid mistake and just can't figure it out. Any help is appreciated!
EDIT: Here are the contents of example.jar, according to vim:
" zip.vim version v27
" Browsing zipfile /home/ellen/bendersexample2/src/bendersexample/example.jar
" Select a file with cursor and press ENTER
META-INF/
META-INF/MANIFEST.MF
AnnotatedBenders.class
Demo$1.class
Demo.class
Demo$ModelType.class
ManualBenders$1.class
ManualBenders$BendersCallback.class
ManualBenders.class
Model.class
Problem.class
Solution.class
Solution$Verbosity.class
StandardModel.class
Here are the contents of the META-INF/MANIFEST.MF which is in the jar:
Manifest-Version: 1.0
Created-By: 1.8.0_161 (Oracle Corporation)
Main-Class: Demo
UPDATE: Here are the interesting parts of Demo.java:
package bendersexample;
public final class Demo {
/* Some functions */
public static void main(final String[] args) {
/* Some code */
}
}
I changed my MANIFEST.MF to the following:
Main-Class:bendersexample.Demo
And re-generated the example.jar file. I still get the following:
Error: Could not find or load main class bendersexample.Demo
Could there be an issue with how I generate my class files?
To generate the class files initially, I did the following:
javac -classpath .:/opt/ibm/ILOG/CPLEX_Studio_Community128/cplex/lib/cplex.jar *.java
Please let me know what else I should try! Thank you
The problem was just that had the Manifest in the /bendersexample folder and the was creating the .jar in this folder as well! I just needed to move that stuff into the parent directory and everything worked fine!
The final Manifest file used bendersexample.Demo as the Main-Class, and the jar was created and run from /bendersexample 's parent directory.
If anyone runs into this problem I would recommend taking a look at your project's structure before trying anything else, because this turned out to be a very easy fix!

placing and compiling a .java file in working directory

just started learning java, i'm looking to save a .java file (TextIO.java) into my working directory. (which file makes up my working directory exactly?)
and then run it through cmd so that Eclipse can use the class.
I tried doing that by saving (and running thru cmd) the file in the folder containing the programs (yah right) i have written already. But Eclipse still cant find the class.
Can i get some help with this?
thanks
You have to create an Eclipse Java project first. The you can copy the file from file system explorer window and paste it on the Eclipse Package Explorer tree. Eclipse projects compile all files in their source folders automatically by default.
The TextIO.java clas you mentioned has a javadoc saying: ... TextIO provides a set of static methods for reading and writing text... This means you use it as a toolkit rather than executing as an application. Given TextIO.java and you class TextIOUtilizer are both in the root of your classpath (source folder in Eclipse), here is how you call a TextIO method:
public class TextIOUtilizer {
/**
* #param args
*/
public static void main(String[] args) {
// code
TextIO.skipBlanks();
//code
}
}
Now you run TextIOUtilizer as a Java application - it has a main method. Hope this helps.

eclipse cannot find main method from given launch configuration

I went to eclipse>export>java>runnable jar file
I changed launch configuration to my project name; destination: desktop.
I tried all three library handling options:
extract required folders into generated JAR
Package required libraries into generated JAR
copy required files into a sub-folder next to the generated JAR.
I did not check anything to do with "ANT scripts".
When I pressed "finish" in the export window, eclipse gave me a error: "Could not find main method from given launch configuration". After pressing the "finish" button in the eclipse export window, the next button was grayed out. It created a JAR file at the destination. When I tried to open it Mac's Jar Launcher (default)(13.5.0), it gave me the message:
The Java JAR file "filename.jar" could not be launched.
Check the Console for possible error messages.
I googled this and some people said to check the manifest.mf file. I extracted it, then: "META-INF>manifest.mf". I opened manifest.mf with textedit.
Inside manifest.mf were the lines:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ acm.jar
Class-Path: .
I am running Mac OSx 10.6.8 snow leopard. I have the latest java available for snow leopard (I think my java is the one just BEFORE java 7).
Also, I'm trying to export a project with:
a .java file that contains all my code
acm.jar
java.policy.applet
Scores.txt
My .java file contains a public void run() {} and JFrames/JPanels. My .java file extends ConsoleProgram.
If there's no public static void main(String[] args) method, the virtual machine (Java) will not know where to start your program. It sounds like your run() method is what you want to start your program. If so, then add (to your main class (.java)) a method: public static void main(String[] args). Re-export your .jar, making sure the launch configuration is set to your Project name, and your main class.

How to display message box using java and JAR

I'm trying to build executable jar file. I'm fairly new to this and not really sure how it is suppose to work. I'm using eclipse 64 bit and windows 7. I'm exporting the project to jar file. This is project code. when I double click the file it doesn't do anything and I'm expecting to see a message box. Can you please tell me what am I doing wrong and help me fix it. thanks.
import javax.swing.*;
public class Starter {
public static void main(String[] args)
{
String st="Welcome";
JOptionPane.showMessageDialog(null,st);
}
}
It sounds as if there is no Main-Class defined in the MANIFEST.MF in your jar file.
There are numerous guides on how to create an executable jar available such as this one.

Categories

Resources