12

In a Java project, there have two java files has main method. The absolute paths for these two java files are:

C:\Desktop\project1\src\com\pre\moveposition1.java

And

C:\Desktop\project1\src\com\pre\moveposition2.java

When I try to setup “Main Class” parameter in “Run Configuration”, what should I setup?

enter image description here

user288609
  • 12,465
  • 26
  • 85
  • 127

6 Answers6

5

Put the (fully qualified) name of the class containing main. For example, if you want to use moveposition2's main (and not moveposition1's) then you'd enter:

com.pre.moveposition2

Also, clicking on "Search..." should give you a list of classes that contain main() methods that you can choose from.

Dave Lillethun
  • 2,978
  • 3
  • 20
  • 24
  • I added it as suggested. But launching the "run configuration" gives the error as "Could not find or load main class com.pre.moveposition2". – user288609 Jul 01 '13 at 04:44
  • I inferred the package name from the directory structure, which _should_ be do-able in Java, but just to be sure... Do these source files both include at the top?: package com.pre; – Dave Lillethun Jul 01 '13 at 05:01
  • I just got out my computer with Eclipse and tried it on one of my projects. It works, assuming you enter the full class name with the right package. Also, the "Search..." button gives you a GUI way to select the class - Just edited my answer to include that part. – Dave Lillethun Jul 01 '13 at 05:10
  • Hi Dave, thanks for your answer. I clicked the "classpath" tag in the "run configuraions" and update the "Bootstrap Entries" to the "C:\Program Files\Java\jre7\". Right now, the "could not load main class" error disappears. However, the eclipse gives the following error message: "Error occurred during initalization of VM java/lang/noClassDeffoundError: java/lang/object" – user288609 Jul 01 '13 at 05:19
  • Yikes! Sounds like a problem your JRE... I suggest checking your Eclipse settings to make sure it's pointing to the JRE (and the right one, in case you have multiple installed). Barring that, maybe there's a problem with the JRE itself? You could try reinstalling that. (But I'd check the Eclipse settings first, since that's easier.) – Dave Lillethun Jul 01 '13 at 05:30
  • can you elaborate which eclipse setting I should check? Thanks. – user288609 Jul 01 '13 at 05:31
  • On your project properties, you could check the Java Build Path, in the Libraries tab. It should list a JRE System Library (or something similar). You can also check the global properties in Eclipse, Java->Installed JREs. There you should see one or more JREs that are installed on your system, and the directory path where they can be found. – Dave Lillethun Jul 01 '13 at 05:58
  • 2
    In case someone else runs into the same problem with "Could not find or load main class", make sure that your main class has the main method with String[] arguments: public static void main(String[] args) {} – Andrei Volgin Dec 05 '13 at 06:15
2

If both classes have a main() method, you can only run one at a time, since they are effectively two distinct programs.

So, in the Run Configuration, you choose either moveposition1 or moveposition2. If you later want to run the other one, just right-click on it and select Run As...->Java Application. You will now have two run configurations for your project.

mikołak
  • 9,605
  • 1
  • 48
  • 70
0

Under Run Configurations, you can create multiple launch configurations under 'Java Application'. Create one with project as project1 and Main Class as com.pre.moveposition1 and try hitting Run.

You should create one more for com.pre.moveposition2 if you want to run that one.

Note: It is best practice to name classes to start with caps letters.

anoopelias
  • 9,240
  • 7
  • 26
  • 39
0

If both classes contain a Main() function, you should setup the class which you want your program to start with.

If only one of your classes has Main() function, setup that class.

ian
  • 193
  • 2
  • 10
0

if the method has a main method look down the Package Explorer, select the file you want to run (that has a main) right click it, selected Run As, select Java application.

mist42nz
  • 97
  • 1
  • 8
0

if it's a maven project look for your class under target/classes folder. You certainly open the automatic build also.

binboavetonik
  • 157
  • 1
  • 5