0

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

ellen
  • 571
  • 7
  • 23
  • Once you created the jar what are the contents of the jar look like. You can open it using winzip or 7zip. I am more interested in the folder structure, files in it and manifest file contents – Tej Apr 20 '18 at 04:50
  • @Tej Added the folder contents above, let me know if there is more information I should post – ellen Apr 20 '18 at 05:16
  • Please post the contents of manifest file that is there in the jar – Tej Apr 20 '18 at 06:06
  • Also have a look at the question https://stackoverflow.com/questions/13030675/could-not-find-or-load-main-class-with-a-jar-file which might help you further to narrow down the issue – Tej Apr 20 '18 at 06:11
  • @Tej Sure, I'll take a look and add the info above – ellen Apr 20 '18 at 17:01

1 Answers1

0

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!

ellen
  • 571
  • 7
  • 23