Could not find main class, jar in eclipse - java

I need to make an executable jar for my ending project and I keep getting the "Could not find main class"....
I've tried with export > runnable jar and export > jar file (and I did chose the main) in both cases I get the same error.
Details in the runnable:
My run configurations for the main of the project:
The dependencies in my project:
I'm totally lost and i don't know where's the problem

Verify that you can start your application like that:
java -cp myjarfile.jar Principal.java
You're double-clicking the file on a windows explorer window? Try to run it from a console/terminal with the command
java -jar myjarfile.jar
Please check below link for further clarification . Running JAR file on Windows

Related

I'm compiling and running fine my java project inside VS Code, but how can I compile it and run it in a different machine?

I have this organization inside VS Code: https://imgur.com/a/HXWo1VF
I just click on the "play" button and the app opens just fine. The command that VS Code use to run it is:
$ cd "/home/allexj/Dropbox/ingegneria_del_software/Link to ing_del_softw/codice/VipagePharma/vipagepharma" ; /usr/bin/env /usr/lib/jvm/java-11-openjdk/bin/java #/tmp/cp_8o2vekaqsyood6mnlra3x7qpf.argfile -m com.vipagepharma/com.vipagepharma.App
The build command is not showed, so I don't know how VS Code build it and I am newbie so I'm kinda poor in package and compiling stuff.
Inside /tmp/cp_8o2vekaqsyood6mnlra3x7qpf.argfile there is:
-cp "/home/allexj/.m2/repository/org/openjfx/javafx-controls/13/javafx-controls-13.jar:/home/allexj/.m2/repository/org/openjfx/javafx-graphics/13/javafx-graphics-13.jar:/home/allexj/.m2/repository/org/openjfx/javafx-base/13/javafx-base-13.jar:/home/allexj/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13.jar" --module-path "/home/allexj/Desktop/ing_del_softw/codice/VipagePharma/vipagepharma/target/classes:/home/allexj/.m2/repository/org/openjfx/javafx-controls/13/javafx-controls-13-linux.jar:/home/allexj/.m2/repository/org/openjfx/javafx-graphics/13/javafx-graphics-13-linux.jar:/home/allexj/.m2/repository/org/openjfx/javafx-base/13/javafx-base-13-linux.jar:/home/allexj/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13-linux.jar"
How can I build and run the same code in another machine? I don't know how VS Code has built everything, so I don't know how to do it in a portable way. Say you cloned this repo: https://github.com/All3xJ/VipagePharma/tree/main and you have to build&run it. How to do that?
You can convert project to a Maven project or create a jar file using jar cf jar-file input-file(s) and then run jar file using java -jar <jar-file> command
Guide to Creating and Running a Jar File in Java
Creating a JAR File
You can export your build to JAR from the projects view or by running the command Java: Export Jar....
You can refer to the document for more details.

I cannot run the code with external jar in Terminal but I can run it in any IDE

So I am new to the java.
I have these external two jar files. I properly imported them into my IDE: exclips.
When I use the IDE the code, it is running and using the external jars. However, when I am trying to use the terminal of MAC, it is giving a mistake of no two jars like this
testten.java:3: error: package stdlib does not exist import
In my IDE the testten.java is running and I have the testten.class. but in Terminal
How can I solve it ??
Try exporting the program as a runnable jar file. Then on PC navigate to the location of the jar file in CMD and type "java -jar {Name of jar file}.jar". On a mac navigate to the location of the file in Terminal and type "java -jar {Name of jar file}.jar". Hope this helps :) if the issue continuous try checking out this: Java Test package does not exist

How can I start a non-runnable Java file (.jar) with a console (cmd) open to display outputs without using something like mvn exec:java

I know there's threads similar to this one have been posted before but after reading through many of them, I still haven't found my answer.
What I have is a .jar file and I want to be able to run it with a command line output. I've tried java -jar .jar but it just closes the window instantly and I don't want to use mvn exec:java as I need to just have the jar file without pom.xml and also not need to have maven installed on the running machine.
Any ideas of how to do this?
You need to run the program indicating the jar in the classpath.
Assuming you are in the folder with the jar:
java -cp myjar.jar mypackage.MainClass
The reason the window closes immediately is because with the -jar option, java will look for a manifest file to know which is the main class. As the jar is not executable, java prints a message saying it didn't find a manifest file and exits, which closes the window.

Multiple ways of creating executable jar not working

I'm trying to make a jar that the user can double-click to run (not a .exe just a jar that can be double-clicked). The problem is while this question has been asked many times none of the answers have worked.
I think the problem is not on my system, because other jar files on my system (that I didn't create) run fine. This makes me suspect I am doing something wrong.
First I tried this example.
However when I tried running the jar I made with that solution with:
java -jar HelloWorld.jar
I got the error: "no main manifest attribute in HelloWorld.jar"
I suspect the error is with my manifest file so here is is:
Manifest-Version: 1.0
Main-Class: main.Main
I also tried: "File --> Export --> RunnableJar" in eclipse but while that allowed me run to my jar from the command line when I tried double-clicking it I got the following message: "A Java Exception has occurred".
At this point, I don't know what to do to make my jar double-clickable.
What can I do to make my jar double-clickable?
Update: I used a new command:
jar cfe HelloWorld.jar main main.class
Now when I try to run this new jar from the command line with
java -jar HelloWorld.jar
I get the following error: "Could not find or load main class main"
If you did everything as described in that example, I think that there is no association with the *.jar file in the registry. Check it.
Also it maybe that two versions of Java installed.
Check Right-Click -> Open With. Java Runtime should be listed there.

How do I run a .jar executable java file from outside NetBeans IDE?

How do I run a .jar executable java file from outside NetBeans IDE? (Windows Vista). My project has a .jar file created by Netbeans. We'd like to run it. Either: how do we run the file or how do we create a 'proper' executable file in NetBeans 6.1?
Running a jar is as simple as
java -jar filename.jar
as Laplie said, java -jar your.jar
EXECUTABLE file : see this thread for answers How can I convert my Java program to an .exe file?
One of the best technique to run the jar file or jar jar file
Create the jar file by jar command choose your jar filename
Java_Jar_File.jar
Run the jar file us the command like java -jar Java_Jar_File.jar
You can do it from the command prompt if java isn't in your path by finding the full path to your java install, something like:
C:\java\java.exe -jar C:\jar_you_want_to_run.jar
or if java is in your path:
java.exe -jar jar_you_want_to_run.jar
This will run the jar produced by netbeans.
First, make sure you set the Main Class in your NetBeans project properties dialog.
Then, you can either
Double-click the jar file (This should work on any machine with an installed JRE)
or
Make sure that java.exe is in the path (or replace java below with the fullpath and file name of the executable), put the following in a batch file:
java -jar filename.jar
Then you can double-click the batch file instead of the jar (useful if you have people unaccustomed to using naked jar files)
OR
You can go down the path described here How can I convert my Java program to an .exe file? to build a .exe for windows.
Make a BAT file with
java -jar filepath.jar
from a command prompt you can run this command: java -jar your_jar.jar.
To run a java jar file its file association should be properly configured. There is a free tool called jarfix to fix all jar file association problems. Run this file to fix all jar issues.
In the project properties dialog in NetBeans you need to set the Main Class - otherwise the generated .jar will not be executable. Then, as already indicated, either double clicking on the .jar or the command java -jar will start the program.
If you have problems with running your jar, make sure you are trying to run your current version to get newest version in Netbeans: GoTo Run Menu --> Clean and build

Categories

Resources