Find Fault in Java Program Launch? - java

I created a quick program in Java just to get started with the GUI-based elements of Java before I start writing the actual program. In Netbeans, everything compiles perfectly, and the GUI has all of the elements I put into it, so there is not problem when I am using Netbeans.
However, upon building and packaging into the JAR, I have no result on open. I have tried to use Webstart and everything else I could think of, but I get no different result. The JAR does, however, change size along with the varying of my program, so I know something is working, I just must be missing a file or something of that nature.
EDIT:
The manifest says the following after the build (This is inside the JAR):
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.)
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
Main-Class: com.nmagerko.Execute

I have found the answer.
Using help from #Mocker Tim, I found that using java -jar program-name.jar allowed for details that Netbeans did not catch.
This ended up being the fact that my path to my icon had one too many "/"'s, and removing that one slash allowed for the program to be jarred correctly.

Related

"Could not find the Main class" when opening a .jar file

When I try to open a .jar file by double-clicking it I get this message that "Could not find the Main Class", however it works when opening it with the command line.
I've been looking for a solution for some days, I found a lot of people asking the same questions, some of the answers I found suggest that the main class is not declared in the manifest.mf, however I checked it and it is declared, here is my manifest.mf
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_13-b20 (Oracle Corporation)
Class-Path:
Main-Class: my_pack.Mainprog
[empty line]
[empty line]
I tried to run the jar that NetBeans builds, also with the fat jar plugin for eclipse and always get the same error.
What I would do is:
Create a dummy project in your Netbeans and export it as a runnable jar. Then take your manifest and the dummy-project manifest and compare it.
Manifests are really a pain in the code. They need to be absolutely correct formatted. Take care on trailing whitespaces, missing/additional empty rows etc...
Maybe check the Java Version, you are using. Make sure, that your command line uses the same one, like setup in your environment variables. Maybe there are doubled declarations...
Which java.exe is associated with *.jar files in windows explorer? Try exactly THIS java.exe to run your jar from the CLI. Same issue then?
Good luck!

Creating a JAR file and running on Raspberry PI

This is a very common question and there are chances that this might be marked as Duplicate, but even after reviewing a lot of answers and posts from stackoverflow and other communities, the problem just doesn't solve.
I have created a Project in my NetBeans IDE 7.3 and I Build the Project to get the JAR file by pressing SHIFT+F11. The next step, I perform is through WinSCP I copy the JAR file from my Windows Machine to the directory (/home/pi) in Raspberry PI.
Now, I am in the directory in my Raspberry PI where the JAR file is present and in the terminal I run the command
java -jar ProjectFinal.jar
I get the error "Could not find or load main class".
The contents of my JAR are as shown below (in the form of directories):
lib (contains all the libraries)
META-INF (contains a MANIFEST.MF file and the contents looks fine) and the contents are shown below:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_17-b02 (Oracle Corporation)
Class-Path: lib/super-csv-2.1.0.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: projectfinal.ProjectFinal**
and finally projectfinal (contains all the class files). I tried to change the contents of the MANIFEST.MF file by following this link. But this couldn't help me either.
Please suggest me any ideas that could get me out of this. It could be really helpful. I can share the code if it is needed.
I am using JRE version 7.
Any questions/inputs/ideas is deeply appreciated.
Please help.
Thanks a lot in advance.
I don't know about NetBeans but Eclipse IDE has an option to export into a "Runnable JAR File" instead of a normal JAR file.
I had the same issue and it solved my problem.
If not then follow this:
The text file must end with a new line or carriage return. The last
line will not be parsed properly if it does not end with a new line or
carriage return.

Need help packing an executable .jar in java

Ok, I have this file I use for debugging purposes while I'm writing some code. However, I am looking to speed up the process by packing it in an executable .jar file. I have been creating a jar file via eclipse, and attempting to run it as is.
The contents of the jar (Derivitave Quiz.jar) are as follows:
DerivQuiz.java
DerivQuiz.class
Meta-INF (empty folder)
pass.txt
Also, would I have to re-write it to use a non-command line output, such as Jpane/JOptionPane, instead of System.out.print()?
My current code works fine in Eclipse, but not after porting to a jar.
I think you are making this far too complicated, especially if you're flummoxed by a task as simple as creating a JAR file.
All you need is javac.exe and jar.exe that come with the JDK. Look at this tutorial to show you how to do it properly:
http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
I did have to change all my print statements to use JOptionPane.showMessageDialog() instead of System.out.print().
Upon further inspection, the jar was being created without a manifest file, not allowing the jar to run.
I fixed this by adding a manifest file under META-INF/manifest.xml, with the following code:
Manifest-Version: 1.0
Created-By: 1.6.0_27 (Sun Microsystems Inc.)
Main-Class: MCExploit
Adding this allows the jar to run flawlessly, in addition to the other adjustments I made.
Duffymo's answer is good. But let me add something: if you're using Eclipse, you simply go to File -> Export then choose 'Executable Jar', and it does it automatically. Here's a nice guide for doing it a few different ways (including with Eclipse):
http://viralpatel.net/blogs/2008/12/create-jar-file-in-java-eclipse.html

Having trouble exporting runnable JARs from Eclipse

Jar available at http://dl.dropbox.com/u/10919879/roller.jar
After writing and testing my program with Eclipse's built-in run Utilities, I decided it was time to export and distribute. So I go to File>Export>Runnable JAR and designate the class whose main method I want to invoke. Everything seems to be going off without a hitch, until I try to actually run the JAR.
Error: Could not find or load main class C:\Users\Matt\Documents\My Dropbox\Publ
ic\roller.jar
Contents of roller.jar
dice/Roller$RollData.class
dice/Roller.class
META-INF/MANIFEST.MF
contents of META-INF\MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: .
Main-Class: dice.Roller
What's going on, and how do I fix it?
It's not a problem with your jar. It's a problem with OS setting that launches jar files incorrectly. I don't know if currently on Windows after installing Java it will be correctly configured automatically and why it isn't on your system. It may require editing registry to set jar files to be launched with java -jar command.
You can use application that converts JARs to EXEs if you're going to distribute it only to users with Windows.

Could not find the main class (on other computers)

I finished writing a java program and am ready to export it. I made a runnable jar from Eclipse. Running the jar works just fine on my computer, but throws the "Could not find main class" error on any other computer (including my other computer that I write Java on).
Whenever I search around for other people having the problem, it's always the same answer: Check the manifest file. I'm not seeing any problem with mine (Plus, can't imagine why it'd work on my computer but not someone else's)
Manifest:
Manifest-Version: 1.0
Main-Class: my.quick.monster.QuickMonster
And I've also tried:
Manifest-Version: 1.0
Class-Path: .
Main-Class: my.quick.monster.QuickMonster
Both work for me, but not other computers.
Thinking about the things that might be going wrong, here are a few other things to check:
make sure that there are no spurious characters (tabs, spaces) at the end of the lines.
check that the main class is actually in the JAR file with the right name.
on the machine that works, try changing your current directory and seeing if it still works.
check that you are using the same version of Java on each machine. Run java -version to check.
make sure that you are running it as an executable JAR; i.e. as java -jar foo.jar not as java -cp foo.jar.
(One theory is that the JAR is working on the one machine in spite of the manifest; e.g. that it is finding the class via the classpath in your CLASSPATH environment variable or something.)
To summarize for other folks, the OP's problem turned out to be that he had compiled his code with / for Java 7, and was trying to run the JAR on older Java installations. That wasn't working because of the classfile version numbers.
You can compile your code so that it will run on an older version of Java, but you need to use the -target option when compiling, and you ought to use the -bootclasspath option to compile against an rt.jar from the oldest Java version. A typical IDE will simplify this by allowing you to specify the target build platform, but it is worthwhile understanding the technical details, for cases where you are not using an IDE.
(I'm surprised that the java command didn't mention the classfile version number in the error message ...)
Make sure the MANIFEST.MF file contains a blank line at the end. If the Main-Class definition is on the very last line of the file, some class loaders ignore it.
Do not ever use 'eclipse-jar-worked-fine-on-my-computer'. I use maven shade jar plugin which excellently build a ready to run jar with all the dependencies, specified main class, etc.
EDIT:
What is the wrong with eclipse-builded-jar is that you won't been able to build it w/o elcipse. Maven is the common tool widely used to build packages of any kind. It's automated, and means that it can be used in CI environmet, etc. And the goal of a good developer is to write code so that it can be easily moved to CI.
However, if it's not a regular task, assuming to make just once/twice, theen, maybe, 'eclipse' solutio has also some benefits. But, I answered keeping in mind some cases of my past when people build packages in GUI just because they didn't manage to do it in maven.
So, I hope there is enough arguments for maven vs eclipse, so please stop downvote :D

Categories

Resources