Could not find the main class x. Program will Exit - java

I explore other threads on stackoverflow, I explore the web but couldn't find the solution...
so I am posting it here with full "hello world" example.
I am using Eclipse 3.6/jdk 1.6 I write a one class using swt and want to export it as runnable file.
Here is how I am trying to do it:
File->Export->Java->Jar ...in the following dialogs I select to create new manifest and select my mainc class for entry point, export it and run it but get this error:
Could not find the main class co.My. Program will Exit
Here is my Manifest.mf file, with two empty lines at the end:
Manifest-Version: 1.0
Main-Class: co.My
In class I have method:
public static void main(String args[])
And here is download link for whole "hello world" project.
http://www.mediafire.com/?wl6mixmpatpglwh

If I remember good, there is in Eclipse also an option to create a Runnable Jar File.

This will help to solve the problem

Related

java class executing on Eclipse but not in command line

My java class files run in Eclipse but not in command line. I have tried all possible solutions. My code has the following structure:
Client_1/src/filedownload/Client.java
RMI_interface/src/filedownload/Hello.java
The Client.java file is dependent on Hello.java. filedownload is the name of package.
When I compile using the following command, it works.
javac RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
But when I execute the class file in the Client_1/src folder using following command, it does not work.
java filedownload.Client
The error displayed is
Could not find or load main class
I have tried many posts on stackoverflow but I am unable to solve it. I am using ubuntu.
The code structure is
package filedownload;
import ....
public class Client implements Hello, Runnable{
...other functions.....
public static void main(String args[])throws Exception{
}
}
Does your Client class have the main() method ? Where are the .class files after compilation (that is, what's the current directory you're executing the compile from) ? What's the current directory when you try to execute ? What's the classpath when you try to execute ?
Without all that info, there's little chance of anyone being able to get you going (but for the obvious advice of just setting up eclipse and doing everything from within eclipse - letting eclipse take care of all the nitty gritty detail).
(And the questions themselves suggest various possible points of failure in your scenario so look at it.)
All your steps seems to be correct. You didn't share the Client.java code which has main method.
Make sure you follow this main method syntax:
public static void main(String[] args){
...
}
E.g. if you write main without args, it can't be found.
You need to put your classes in a separate folder, separated from your sources.
javac -d bin RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
(folder 'bin' must exist already)
And inside folder 'bin' execute command:
java filedownload.Client

Finding the Main Class

I have my first application written in Netbeans, with supporting libraries. After building the project, the file NetbeansProjects/PDF/dist/PDF.jar runs fine.
I'm ultimately trying to build a OSX app, but think(?) that the first step is to bundle the PDF.jar and the /lib/*.jar files together.
To this end, I'm using JarSplice, but can't work out how set the Main Class. I think it should be found in the manifest.mf file, but it doesn't seem to contain anything. JarSplice requests:
Enter your applications main class file below, complete with any packages that it maybe in.
E.g: my package.someotherpackage.MainClass
Adding System.out.println(main.getClassName()); to my main method gives me "PDF" in the output window of Netbeans.
Can someone tell me how I go about finding the main class, and ideally, because I'm an idiot, exactly what to input into as the main class into JarSplice?
It depends were you put your main class when developing.
Try the class name that contains your main method, if you put your main method in Test.java then put;Test
If your main class is nested in a package then put something like org.package.Test
Once you export your .jar open up the Manifest file and next to Main-Class: should have the main class, just copy and paste

Netbeans GUI wont display

I have designed a simple gui in netbeans on windows but when i try to run it,nothing is showing.My main class is called ModelApp.java and the gui part i designed in netbeans is called app.java. When i try running the jar on command prompt,i get the error
Error: Could not find or load main class ModelApp
Your ModelApp class is probably not in the folder written in the manifest file or something like that.
See JAR Basics
If you want to run your application by clicking on the jar you need to specify the main class in META-INF/MANIFEST.MF file in the jar as it is described http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html . Make sure you have it properly defined.
You should be able to do this in Netbeans during creation of your jar file.
Did you forget to create the main method in ModelApp.java?
public static void main(String[] argv) {
app gui = new app();
}
Normally this should work

Distributing an executable Jar console app

(Using JDK7)
A friend of mine asked me to write him a program for rolling dice of arbitrary number and size. So I did. For simplicity's sake, I kept it as a console app. Now I'm trying to distribute it to him. I tried exporting it as a Runnable Jar with Eclipse's built-in exporter.
After opening it with Java (As opposed to javaw)... Nothing happens. I'm able to take a screenshot of the command prompt before it closes, and it reads:
Error: Could not find or load main class C:\Users\Matt\Desktop\Roller.jar
Contents of Roller.jar
META-INF/MANIFEST.MF
roller/Roller.class
Contents of MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: .
Main-Class: roller.Roller
Contents of Roller.java:
public class Roller{
/* Irrelevant code expunged */
public static void main(String[] args){
//Irrelevant code expunged
}
}
I want to get this in a state where I can just email him the .jar and he can double-click it to run it. Can anyone help?
In Roller.java, you need to add package roller; to the top of your class to match Roller.class being in the roller folder in the JAR file. After this, you don't even need to specify a Class-Path in your Manifest file.

exporting java jar help from Eclipse

so I have a project that only has one class Main:
public class Main{
public static void main(String[] args) {
System.out.println("asdf");
System.out.println(args[0]);
System.out.println(Integer.parseInt(args[1]));
}
}
so I tried to export this as a standalone jar...here's what I did
created a run configuration for the project...the run configuration runs properly from eclipse...it prints the arguments just fine...
right click on the project, selected export
choose the runnable jar file option
selected my previously created run configuration as the run configuration, chose export destination press finish, etc
but then when I run the program from the commandline:
Main.jar "testtest" 123
nothing came up.....it didn't print out the arguments....nor did it print out the "asdf" I specified in the main() function...
what did I do wrong?
Run java -jar Main.jar "testtest" 123 ?
I think you are getting wrong what exporting as a runnable jar means: This does not create an executable file. It just creates an archived file with all your compiled classes and some special meta information about which class to run when invoked with java -jar JARNAME.
Note that this also means all users of your program still need to have a java runtime installed.

Categories

Resources