java project problem - java

I have a java project. I can run it through command prompt but can not able to run through Eclipse or NetBeans. When I run it, the error come main class is not found.
What can i do ?

How are you trying to run it in Eclipse and Netbeans? Basically you need to tell them which class to execute - which class has the main method in.
In Eclipse you can just go to the relevant class and hit Alt-Shift-X, J to launch it.

A few steps for eclipse
create a new project: Menu File/New/Project...
place your java source in the src folder of your project
through the context menu (right click the project name in navigator) you define the build path, and add required libraries.
now your code should be ready to run using the green (>) button

Is your project using libraries? I had the opposite problem where I could run my program in Netbeans and not from the jar (or command line) because the libraries were in my Netbeans folder and not my "distribution" folder.
EDIT: By libraries I mean third party libraries.

When you create a project in NetBeans, it will create a default Main class for you, complete with a main(String[] args) method. To access your code, just rename your class main method, copy it (and any dependnt classes into the project and change the package names to reflect the project name) and instantiate the class containing it in the default NetBeans main method and call the renamed main method e.g. if your class is called HelloWorld and the main method was renamed "hello" the call would look like:
HelloWorld hw = new HelloWorld();
hw.hello();
Simples :)

Related

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

Error: Selection does not contain a main type

I am trying to run some java files in a new project. So I make the project, put the files in it and I try to run the main file so my game starts.
I get an error that says selection does not contain a main type.
I have tried several ways to run it:
Some say to launch eclipse again, tried this a dozen times.
Somewhere else someone pointed to open a new project and make a build path to the old project.
Didn't work either.
I am pretty sure it must work because I ran it a few hours ago at school. How do I get this working? Thank you in advance!
Right click on the folder where you put your main class then click on Build Path --> Use as Source Folder.
Finally run your main file as java application. Hope this problem will be solved.
If the option 'Use as Source Folder' is not visible then inside the 'Build Path' select the option 'Remove from Build Path'. This will allow 'Use as Source Folder' option to appear in the 'Build Path'.
The other answers are all valid, however, if you are still having a problem you might not have your class inside the src folder in which case Eclipse may not see it as part of the project. This would also invoke the same error message you have seen.
I hope you are trying to run the main class in this way, see screenshot:
If not, then try this way. If yes, then please make sure that your class you are trying to run has a main method, that is, the same method definition as below:
public static void main(String[] args) {
// some code here
}
I hope this will help you.
The entry point for Java programs is the method:
public static void main(String[] args) {
//Code
}
If you do not have this, your program will not run.
I resolved this by adding a new source folder and putting my java file inside that folder. "source folder" is not just any folder i believe. its some special folder type for java/eclipse and can be added in eclipse by right-click on project -> properties -> Java buld path -> Source and add a folder
Few things to check out:
Do you have a main package? do all of your classes are under this package?
Do you use a main class with public static void main(String[] args)?
Do you declare: package ; in your main class?
You can always clean the project before running it. In Eclipse - Just go to Project -> clean then run the app again.
I ran into the same problem. I fixed by right click on the package -> properties -> Java Build Path -> Add folder (select the folder your code reside in).
I am running eclipse from Ubuntu. Had this same problem and was able run the program through terminal. So I just moved the existing public static void main(String[] args) { just below the class declaration (it got automatically formatted by eclipse) and the next launch was successful. Then moved the main method back to where it was before and it worked fine this time.
I had this problem in two projects. Maven and command line worked as expected for both. The problems were Eclipse specific. Two different solutions:
Project 1): Move the main method declaration to the top within the class, above all other declarations like fields and constructors. Crazy, but it worked.
Project 2): The solution for Project 1) did not remedy the problem. However, removing lombok imports and explicitly writing a getter method solved the problem
Conclusion:
Eclipse and/or the lombok plugin have/has a bug.
Looks too late to answer but might help someone,
Having same problem i solved it by following steps:::::
Select Main class in eclipse then click on Window in menu bar,
Window-->Show view-->Select Outline
Right click on main(String[]):void then Run As --> java Application
By doing this you can run the main method directly. This worked for me
Right Click > Run AS > Run Configurations
In this screen if your "Main class" Text field is empty, then add the class name by clicking "Search" button on the right side of the text field and choose the class file. And then click "Run" button on the bottom of the configuration screen. That's it
You must place all your files (file.java) under the root folder SRC.
Make sure the main in public static void main(String[] args) is lower case. For me it didn't work when I had it with capital letter.
Put your Main Java class file in src/main/java folder and check if there is not any error in 'Java Build Path' by following right click on project and select Java Build Path->Source.
If you are working with a Maven project you have to understand the fact that directory layout is bit different. In this the package name must be src/main/java.
For this update your source folder by right click on project root folder -> properties -> java build path -> source tab. Here remove all other source folders as they might have been added in wrong manner. Now, select project /src/main/java as the source folder. Add and apply the changes. Now refresh your workspace using F5.
This should fix the issue of not recognizing a main type.
I ran into the same issue and found that there was an extra pair of braces (curly brackets) enclosing public static void main(String args) { ... }. This method should really be at the top scope in the class and should not be enclosed around braces. It seems that it is possible to end up with braces around this method when working in Eclipse. This could be just one way you can see this issue when working with Eclipse. Happy coding!
I had this happen repeatedly after adding images to a project in Eclipse and making them part of the build path. The solution was to right-click on the class containing the main method, and then choose Run As -> Java Application. It seems that when you add a file to the build path, Eclipse automatically assumes that file is where the main method is. By going through the Run As menu instead of just clicking the green Run As button, it allows you to specify the correct entry-point.
When you save your file, make sure it has the extension .java. If it does not, Eclipse won't know to read it as a java file.
I had this issue because the tutorial code I was trying to run wasn't in the correct package even though I had typed in the package name at the top of each class.
I right-clicked each class, Refactor and Move To and accepted the package name suggestion.
Then as usual, Run As... Java Application.
And it worked :)
You must check this as well, Go to Java build path -> config build path, check that JRE System Library [Java SE -version] is check marked, try running the code again. This fixed my issue.
In my case I was using ant to perform the build, and clearly had the class setup in the build.xml file:
<target name="jar">
<jar destfile="ec-stats.jar" includes="bin/**,src/**">
<manifest>
<attribute name="Main-Class" value="my.package.MyStatistics" />
</manifest>
</jar>
</target>
But it still kept giving me the same error, until I right-clicked on the folder in Eclipse and chose Build Path -> Use As Source Folder.
See also Error: Selection does not contain a main type
Solved the issue as following:
Going in Properties of the Project
1.1) Inside "Java Compiler": chose the: "Compiler compliance level:" to a specific version of JDK (in my case I choosed 15)
1.2) Inside "Java Build Path", in the tab "Libraries", at the Modulepath: change the "JRE System Library" to the same of the version you choosed at step 1.1 above (in my case I picked JDK 15)
Run the java file that contains a main method
This can be resolved in two steps if you are using Eclipse IDE.
👉 Step I: Right click on the project and click Maven > Choose Up-date Project
👉 Step II: Select the project the click Ok button
Hope this helps.✔
This error usually occurs because jdk is not selected for the project. I had the same problem and this worked for me.
Make sure that your class file is inside src folder then follow below steps
Step 1: Right click on project folder
Step 2: Build Path --> Configure Build Path
Step 3: Select "Java Build Path" from the left corner of the window
Step 4: Under "Order and Export" Tab
Step 5: Click on any of the jdk available check box to select it.
Step 6: Click on Apply and Close.

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

Java: simple JAR project, when run, cannot find an imported class in a second simple JAR project even though second JAR passed via -classpath

I have written two simple Java classes (one of them containing "main()", and the other called by "main()").
Class #1 (containing "main()"):
package daniel347x.outerjar;
import daniel347x.innerjar.Funky;
public class App
{
public static void main( String[] args )
{
Funky.foo();
}
}
Class #2 (called by "main()"):
package daniel347x.innerjar;
public class Funky
{
public static void foo()
{
System.out.println( "Funky!" );
}
}
The above classes appear in different project root folders, and use Maven as the build system (each project has its own POM). The pom.xml file for the main project includes the proper entry to add daniel347x.outerjar.App as the main class, and it properly includes the dependency on daniel347x.innerjar. Both projects build successfully into JAR files.
I use NetBeans to wrap these as Maven projects (in fact, I used NetBeans to create both projects). When I run the main project from within NetBeans, it runs successfully and I see Funky! as the output.
However, when I attempt to run the main class straight from the Windows command line (cmd.exe), passing the JAR file containing Funky on the command line's classpath, as such:
java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar" -jar "P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar"
... I receive the dreaded NoClassDefFoundError:
Exception in thread "main" java.lang.NoClassDefFoundError: daniel347x/innerjar/Funky
at daniel347x.outerjar.App.main(App.java:7)
I have carefully confirmed that, inside the innerjar JAR file noted above containing Funky, that the path structure is daniel347x\innerjar and that inside the innerjar folder is the Funky.class file - and this file looks correct within a HEX editor where I can see the ASCII strings representing the name of the class.
The fact that the class can't be found defies my understanding of Java, which I thought allows you to pass JAR files as a -classpath parameter and it will be able to find classes inside those JAR files.
This very basic point has me flummoxed - an answer that explains what I am doing wrong would be greatly appreciated.
The classpath is ignored when using the -jar option. A way to run your app would be java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar";"P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar" daniel347x.outerjar.App
Perhaps a better approach would be to add a manifest file to the Jar that specifies the class path of the dependent Jars by relative paths. Then..
java -jar "P:\_Dan\...\outerjar-1.0-SNAPSHOT.jar"
..should do it.
Double clicking the main Jar will also launch it. That is mostly useful for GUIs.

Creating jars with multiple main classes in netbeans

I'm am creating a VoIP client server pair in java using the netbeans IDE. I would like to use only 1 project but I am unfamiliar with with how netbeans would create the Jar file. I need a to if this is possible and if so, how to set it up.
The JAR File Specification allows only one Main-Class attribute per JAR, but the JAR may have an arbitrary number of classes that declare a main() method. Any such class will be included in the project's Build Packaging property, unless specifically excluded.
As a concrete example, H2 Database includes these classes with main():
org.h2.jdbcx.JdbcConnectionPool
org.h2.tools.Backup
org.h2.tools.ChangeFileEncryption
org.h2.tools.Console
org.h2.tools.ConvertTraceFile
org.h2.tools.ConvertTraceFile
org.h2.tools.CreateCluster
org.h2.tools.DeleteDbFiles
org.h2.tools.Recover
org.h2.tools.Restore
org.h2.tools.RunScript
org.h2.tools.Script
org.h2.tools.Server
org.h2.tools.Shell
Addendum: Apparently, my junk-drawer project needs maintenance.
$ find scratch/src -name \*java | xargs -J % egrep 'main[ \t]*\(Str' % | wc -l
109
I don't know how Netbeans work, but it should be no problem putting more than one main class in a JAR. Actually a main class is just a class having a main method and a JAR is a collection of class files.
The only restriction is that there can only be one class that will be started with double-clicking the JAR.
To start the each class you must not use the -jar option, but provide the full class name.
For example, if you have a Client and a Server class in your JAR, the Client is started by
java -cp file.jar Client
and the Server by
java -cp file.jar Server.
An option is to create a third starter class used to start either the server or the client based on a command line argument (or a GUI window).
I believe the purpose of a 'main class' means that the eventual JAR' manifest file, will label one class as being the 'class to run': so that the end-user can just double-click it* or run a simplified commandline like:
java -jar <jar>
rather than having to specify the whole package/class name like:
java -cp <jar> com.yourcom.package.classname
If I'm right, then I don't see how it would make sense to have more than one main class ?
Maybe I misunderstod your question - or there is another purpose to the 'main' class?
If you mean having two classes which have a 'main' method - then this is fine - the end user can launch any of the classes by name - and so long as they have the standard main method sig, for instance:
public static void main(String[] args)
it should just work.
*(on Windows at least, and whether that works also depends on which JRE they have and probably other things)
In netbeans, if you want to create jar file of your project with your selected main class then ,
1. right click on your project (If you couldn't find your project then go to window -> Project from netbeans menubar )
2. Go to set configuration -> Customize.
3. Select run from categories, In main class write your packagename.classname (ie you have created file xyz in mypackage, then write mypackage.xyz)
4. click ok.
5. Again right click on projectname, select clean and build.
6. Your jar file is created at location of your projectname -> dist -> projectname.jar

Categories

Resources