"Could not find main class" on .jar execution [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running Jar file in Windows
I created a .jar file for a small GUI Java project with NetBeans. It runs fine from command line. I have .jar files associated with javaw.exe in the JRE. But when I double click the .jar file I get an error:
Could not find main class.

The fact that double clicking is saying that it cannot find the main class means that a JRE is installed. That is NOT the problem. It also means that a suitable shortcut exists. That is NOT the problem ... either.
The problem is (I think) that the JAR file has not been correctly created as an executable JAR file. An executable JAR file must have a Main-class entry in its manifest that tells java.exe or javaw.exe which class contains the "public static void main(String[])" method that is used to start the application. Either the manifest entry is missing, or it refers to a non-existent class.
Since this is your program, you need to take a closer look at the way that you are creating the JAR file. You most likely need to tell NetBeans which class to use as the entry point / "main" class.

check this answer: Running JAR file on Windows.
In all likelihood, you'll need to supply the javaw.exe" -jar "%1" % part in a shortcut, and that will fix the problem.

Related

How to make my jar file executable by double click to open terminal

Okay so basically, every time I've seen this question asked it doesn't have the answer I'm looking for which is why I'm trying to ask myself.
Basically, I made a java project on BlueJ (required by school, sadly. But I can export it elsewhere if that's the problem (preferably IntelliJ or Visual Studio Code)), but essentially, I want to make it so I can simply double click my .jar export and it will open a terminal window and launch. When I try this however, I get this error: First Error
And when I click Ok then this appears:
Second Error
Now, if I want the jar to run on terminal, doing java -jar jarFileName.jar works perfect, but what I want is for that to happen when I double click the jar file; for it to launch and run from the terminal.
All the other answers I've seen for this problem blame the users computer and usually tell them to install some version of java or whatever, but this isn't the problem. I have other jar files that I can run perfectly fine, like Minecraft Forge installer or Minecraft Spigot installers (sorry only examples are Minecraft, not much else uses Java). And also this was for a uni project and no one else in my field know how to get their jar to be executable, nor does this file work for anyone else.
So yeah, I know it's a problem with how the jar was compiled or something and not what java is installed my PC.
I just want to make it double-clickable for the convenience of when I send it to friends, but if anyone knows another way I can get around this problem then that's fine. For example I know I could make a Bash file which simply does the java -jar jarFileName.jar for me or something, but I'd rather have it all as one jar file, so I don't know if this can be incorporated into the file but yeah.
Also I'd really like a solution which doesn't involve downloading external programs or whatever, since all I want to do is send this file to my friends and have them just double click it to launch.
TL;DR, what do I have to do to make my jar file actually executable by double clicking it. Thank you.
To make a jar file executable you have to set the entrypoint
You can do this by passing the e option to the jar command when you create the jar file, for example:
jar cvfe myapp.jar com.mycompany.myapp.MyApp com\mycompany\myapp
This will add the Main-Class entry in the jar's manifest pointing to the startup class, which should have a main method, for example:
package com.mycompany.myapp;
public class MyApp {
public static void main(String[] args) {
// start up the application
}
}
When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname
META-INF/MANIFEST.MF
The entrypoint is specified by the Main-Class, for example:
Manifest-Version: 1.0
Created-By: 1.8.13_37 (Oracle Corporation)
Main-Class: com.mycompany.myapp.MyApp
If you want to make an existing jar file executable you could unzip it, modify the manifest and zip it again (and rename it so it has the .jar extension - a jar is just a zip).
But I think the errors you show are not caused by the jar not being executable, but by some environment issue, causing the Java application to not find the necessary variables or paths to make the Java Native Integration (JNI) work. To solve this you would have to provide more detail on what you are trying to do with JNI.

How can I make .jar files to run on my computer? [duplicate]

This question already has answers here:
'Java' is not recognized as an internal or external command
(19 answers)
How to run a JAR file
(12 answers)
Closed 1 year ago.
I've searched for a while for a fix, I think I've tried all of the common fixes, I even tried Jarfix. I have JRE installed, and I deleted and reinstalled it again just to make sure, I'm running Windows 10 64 bit. If I double click the .jar just nothing happens, and I've made sure that .jar is set to open with the right file. I don't know what to try so if you have any suggestions, I'll try them, because I'm completely lost.
Whenever I try to run any command starting with "java" is gives me this: 'java' is not recognized as an internal or external command, operable program or batch file.
First of all, Add your JRE to the PATH of Windows.
When you add it there, running java commands in the command line, will be recognized since JRE will also be indexed.
This is how you add it:
set PATH=%PATH%;C:\your\path\to\jre\bin\here
An example:
set PATH=%PATH%;C:\Program Files\Java\jre1.8.0_271\bin
Then go to the folder where your jar file is located. And open a command line there.
Or go there through the cd command (which means change directory to jump to exactly this directory). Example:
cd C:\Program Files\Java\jre1.8.0_271\bin
And then run your jar this way:
java -jar yourJarName.jar
.jar files have the java directory directly inside of them so you shouldn't have to reinstall JRE.
Maybe your .jar file you are trying to run isn't put together properly.
OR there is nothing to run on your file or there is an error in the code.
Those are all ways it could be broken.

How do I generate .class file if I have external Jar dependencies? Javac does not work [duplicate]

This question already has an answer here:
How do I compile a java file that has jar dependencies?
(1 answer)
Closed 8 years ago.
Background: Running on an ec2 instance not eclipse.
I have a java program that has external Jar dependencies. I want to create an executable jar file for this java program (being able to package the external jar files into this jar file would be icing on the cake)
The problem is that for me to use the java cf or cmf command I need to have a .class file. But, when I try javac HKP.java it throws a bunch of errors saying "xyz does not exist or cannot recognize symbol" which I'm guessing is because it has no idea about the external jar dependencies.
I have tried doing javac -sourcepath /home/ec2-user/apps/HKP/lib/ HKP.java, but it throws the exact same errors as before.
EDIT I have tried javac -classpath file1.jar, file2.jar, file3.jar HKP.java -I have also tried this with ";" in between. both return errors saying -bash file2.jar command not found, so instead of continuing the argument, bash is recognizing the multiple jar files as commands.This is the initial problem I had that caused me to post here.
Any help is much appreciated. Thank you!
If you want to compile a java source file into a class, you need to provide all classes which are used in that code on the classpath of the compiler.
If the classes come from (already compiled) external JARs, you typically do that by specifying javac -classpath with a list of JAR files, separated by : (on Linux). But you should really think about using an IDE or at least a maven build file (Which has the benefit, it can even download those JARs for you).
Both (Eclipse IDE and Maven build system) can also generate a ueber-jar with the external classes in there for easy execution.
try to Add the path in system variable and restart your computer.

How to make executable file independent of Eclipse [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 8 years ago.
I have one library, one txt file for import some things with it and one output file to show the result. Only have one java file without any interface. With eclipse all is running well - I am writing into the input file and then it shows the result into the output file. Now I want to make this all independent program (without need to run with eclipse).
How to make it?
An easier way is to make a runnable JAR file.
Go into eclipse choose file > export > runnable JAR.
It will then ask you to choose a class for a launch configuration. Choose the class that has the main method for the desired program. After that you will have a file that will run (like a .exe but just a different format).
If can't get it to work with your input and output files, thats because eclipse has this weird thing that sometimes occurs regarding the location of external sources when exporting to JAR. To fix it, just put the files instead of the src (source) folder, one folder up in the directory. For example, if this is your current directory: C:\workspace\myprogram\src put it in C:\workspace\myprogram
Hope this helps.
The only thing that eclipse is is a software that facilitates progtammi g. But what find the programs is the jvm-java virtual machine. To get to the point eclipse does have a option to convert it to a .jar file which the extension a java program uses to be "independent" from eclipse and open up by double clicking. The way you do this is you right click on the class you want to make independent and I think there should an option that says export to .jar and that should do the trick! Hope this was helpful

.jar file only works when used in Eclipse

I'm using a .jar file which I imported to Eclipse, and when I used Eclipse to run the application is works just fine.
But when I try running it from the command line with just the command java ClassName I get a NoClassDefFoundError message.
The .jar file is in the same directory as the main class I need to run.
I tried using the classpath command in the terminal
java -classpath pack.jar ClassName
but I got the same error but this time it was in the main thread
Exception in thread "main" java.lang.NoClassDefFoundError
Why does my program work in Eclipse but not on its own?
I guess you'll need to use: java -classpath .:pack.jar ClassName.
NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime.
Its Not Even Close To Being equal to ClassNotFoundException n u mustn't confuse them together, so u need to include the whoe path of the .jar file,
use the previously explained technique to do so, or if it was a jar file that want use frequently without using the call to java -classpath .. thing try importing the file into the OS class path directory.

Categories