after a week I am no longer able to call main-classes of jar-files without adding the main-class to the manifest.mf. I don't know what I've broken so far.
I am trying the following:
compiling a jar (for hadoop for example) and calling it with
yarn -jar PathToMyJarFile ClassWithAMain Param1 Param2..
also I could start it with java instead of yarn, that doesnt matter.
My problem is:
The jars I compiled last week now throw an error
no main manifest found in PathToMyJarFile (or whatever the translation from the German Hauptmanifestattribut would be)
I explicit called the Class with main function and setting up a certain class-file is not a solution for my problem, because I also use jar files with more then one class containing a main.
Even a quite simple helloWorld program with just a System.out.println and only one class, I cannot call with
java -jar PathToJarFile ClassName.
Any suggestions?
I tried to do this again with deactivated antivir and firewall with the same result. I also reinstalled my jdk and jre (same version as before).
JavaVersion: 1.8.0_51 and didn't changed since quite a long time
OS: Windows 7U64
Im terribly sorry for that question, I just had to use
yarn jar jarfile class
instead of
yarn -jar jarfile class
incredible stupid of mine, but I mixed calling a jar with java for calling it with hadoop...
Can be closed.
Related
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
So basically I made a terminal-dependent Java program and want to start handing it out, but I can't seem to find a way to do that without giving them my code. When making a .jar file it only works on Windows.
When trying to run on Linux (CentOS) I get
Exception in thread "main" java.lang.UnsupportedClassVersionError:
and
Could not find the main class: MyClass. Program will exit.
I'm using the command java -jar MyJar.jar to run them on both. I am using JavaMail in this program so perhaps that could be an issue. There is only one class and it has a main in it.
If anyone even has any alternatives to using a jar file to deliver the program where the user can't read the code that'd be great too. Thanks!
The JDK that you've actually used to compile and build your Jar is more recent than compared to the one installed on your client's Linux machine. That's when JVM throws an
Exception in thread "main" java.lang.UnsupportedClassVersionError:
Ask your client to report his Java version by running the following on console
java -version
Then if you're using Eclipse set the target runtime of your project accordingly. If you're compiling your Java source files on your own use
javac -target 1.4 source/MyClass.java
To me it sounds like the problem is that you compiled the code on an incompatible version that you're running the code on. For example, you compiled it with JDK 1.7 but the CentOS OS has only the java 1.5 version installed. Here is some more detailed information on the matter.
As for the title of your question, no, you don't have to give them the java source for them to run it. So something else is going wrong. Most likely the answer I gave.
Regarding question title:
Obfuscate your code. Dont print your name directly, use a generator method.
http://en.wikipedia.org/wiki/Obfuscation_(software)
http://www.yworks.com/en/products_yguard_about.htm
http://www.allatori.com/
http://www.zelix.com/
http://www.sable.mcgill.ca/JBCO/
In addition to the version issue others have described, and the fact that you don't need to distribute the .java files, you'll need to set up a Class-Path entry in the manifest file of your application jar file to reference the JavaMail jar file, and you'll have to deliver your application jar file and the JavaMail jar file to users. That can be tricky to get right. You might want to look at One-JAR to solve that problem.
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.
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 ?
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