Java jar running - java

Any idea why
java -jar jarname.jar
works, but double clicking the jar gives a
"Could not find the main class:"
error? Manifest is correctly placed and double clicking was working until recently, but all of a sudden it gives that error. I'm not sure what changes I made though.
EDIT:
The command I used to create the jar was (in cmd):
jar cfe jarname.jar files.Main *
Main is part of the package files.

Add this entry in your manifest file:
Main-Class: com.abc.def.YourMainClass
Hope this'll help!

Some time ago I had a comparable problem. And it was caused by multiple JDK/JRE versions on my machine.
What is used by double-clicking differs from what is used by typing jar in a shell.
I think JarFix could help. Or you lookup what is associated with double-click in the file-associations (reinstall java might also help)

It seems to have been some weird compiler version problem. When I complied the class files using an older version of java, then compiled it into a .jar file, it was able to run.

Related

Program runs in Eclipse, but exported runnable jar file will not open?

It looks like this is a common problem, but none of the previous posts seem to address my issue.
I believe I've narrowed it down to one problem. Any application that uses an InputStream will not open, but all my other applications run fine.
The application runs fine in Eclipse, but the window won't even open when I try to run the jar file.
Task manager shows it pop up for about a second or two, and then disappears.
I have tried all three options for the Library handling upon exporting and none of them fix the issue.
Can anyone explain this?
Run it from the command line. This will allow you to see the exception that's thrown that's preventing your program from progressing.
java -jar YourJar.jar
I know I'm late, but I want to prevent people having to search hours for the same Error I just did. In Eclipse the path-String isn't case-sensitive. In the exported Runnable Jar File it is. So make sure all the pathsnames have the capital letters in the right positions, or to be save don't have capital letters at all.
Setup Your Manifest
If I had to guess, your manifest doesn't contain your main class OR the classpath is not defined.
Main-Class: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Class-Path: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
If you're using a build tool like maven, it needs to be configured to add these properties: https://maven.apache.org/shared/maven-archiver/examples/classpath.html
Execute Your Jar
There are two ways to run an executable jar. As an executable jar you need to define the Main-Class and Class-Path in your manifest:
java -jar YourJar.jar
You can also skip setting up your manifest and define your classpath and main class through the JVM's parameters.
java -cp=${PATH_TO_JAR} main.package.MainClass

Can't find or load main class JarSplice

Hello fellow programmers! I am writing a game at the moment, that uses the following libraries: LWJGL, OpenGL, Slick. I know the problems with simply exporting a JAR file from eclipse when using libraries like that, and I do use the JarSplice program to make a fat JAR. This has worked fine for about a month now, however, just recently it stopped working. Whenever I export the JAR from eclipse, and make it a fat JAR using JarSplice, I get this error:
Could not find or load main class Game.jar
META-INF (Located in the fat JAR):
Manifest-Version: 1.0
Launcher-VM-Args:
Launcher-Main-Class: cubix.main.Cubix
Main-Class: org.ninjacave.jarsplice.JarSpliceLauncher
Please note that the main method of the program is located in the Cubix.class, which is located in cubix.main.Cubix'
I have never came across this error before, and it is driving me insane. If any additional information is required, please feel free to ask.
EDIT:
I might also add that if I export any other project this way (using the same libraries and using jarsplice to build a fat JAR) it works just fine. I am only experiencing this problem with this specific project in eclipse. However, when I run Cubix in eclipse debugger, it runs just fine.
Try couple of things :1 check the content of the file, use jar -tf see everything is present2. can you try running this file from command/terminal.3 if all above is good, compile Cubix.java from terminal and place this .class file in jar file manually and check.
I'm sorry for posting this. The problem wasn't that it couldn't find the main class but rather I had typed the command line wrong. I typed: 'java Cubix.jar -jar' rather than 'java -jar Cubix.jar'. Also, after fixing the command line, it gave me the stack trace for the error that was overlooked by eclipse, and that one of the textures that needed to be loaded was case sensitive.

Java Program only runs in IDE, not exported as a Jar

I can run my software without any problems in my IDE, but when I try to compile it to a jar, and try to run it, it says that the main class can't be found. I succeeded to make it work perfectly several times in the past, but I found this problem few time ago, so I can't really know when it went wrong.
The jar has a META-INF, all properly set up, like in my older versions, and the main class is there. Other peoples can't compile too, so it's not just me. The code is on Github, so that could be a tool to help me solve this.
But I want to know, just by reading this, is there some common mistakes that could cause this, or this is uncommon?
EDIT:
I'm using IntelliJ IDEA
and I will check if the versions are the same.
EDIT2:
They do have the same version, and it's exporting an executable jar with the correct main class defined, as I have beeing doing for all the previous versions.
Check that the MANIFEST.MF is present in the META-INF folder of the jar, and make sure that the line :
Main-Class: yourMainClass
is present inside the file, then make sure that you start the jar with this command line :
java -jar yourjar.jar
Have you tried to launch the jar file specifying the main class ? For example :
java yourjar.jar yourMainClass
Does it work in this way ?

javac: package org.apache.derby.client.am does not exist

I'm trying to compile a java program that is using JavaDB/Derby. On the command line I can run
java org.apache.derby.tools.sysinfo
without errors, but the following line in several of the files causes the error in my question title:
import org.apache.derby.client.am.SqlException;
causes
package org.apache.derby.client.am does not exist
I've done a fresh installation of JavaDB, but I don't think that matters. I've compiled this project once before, and I KNOW I didn't have JavaDB installed. I just had a directory at the top level of the project folder called lib with all of derby's .jar files inside. And I'm pretty sure I didn't have to set any environment variables either.
How can I fix this error? If I need to provide any more information, I will be happy to do so.
I'm using Windows 7 and jdk1.7
Sounds like you have an issue with the JavaDB JARs not being on your classpath. Make sure you specify them using -cp or -classpath on your javac command.

Running Jar on Snow Leopard with classpath

I'm running snow leopard and I just wrote a Java class on eclipse. The eclipse project references a user library which itself points to a bunch of jar files I've got somewhere in the system. When I run the app through eclipse, everything goes smoothly.
Then I export the class as a jar file and try to run it form the terminal by typing:
java - jar myApp.jar
It throws a java.lang.NoClassDefFoundError exception, meaning that it can't find the libraries I try to reference.
Knowing that my user library jar files are in /Users/myname/tempJars, I also tried to either mention the classpath using the -cp option
(java -cp /Users/myname/tempJars -jar myApp.jar) or to directly reference it in the manifest file. Both attempts failed and the error is the same.
These libraries are Java 1.5 libraries, so I thought I should try and reference another java version by mean of the JAVA_HOME environment variable. I built the following script:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$CLASSPATH:/Users/myname/tempJars
java -jar myApp.jar
Again, no good. I googled how to execute jars in snow leopard, found the 32/64 bit big deal in some places and even tried executing with the -d32 option, but still to no avail.
Since the same code executes just fine in eclipse, I'm pretty convinced it's just a matter of setting up the JVM so that it includes the user libraries correctly.
Could anyone help me with this?
Thanks in advance.
You need to list the JARs themselves out on the classpath, not just the directory containing them. (Listing the directory is for when you have unarchived .class files in the package hierarchy lying around)
You will probably need to do -cp /Users/myname/tempJars/libA.jar:/.../libB.jar
I believe some (but potentially not all) JVMs support wildcards so -cp /Users/myname/tempJars/* or some variant thereof may work.

Categories

Resources