11

I get confusing with Eclipse, when I import a project from Git, then I click on a java file, which has correct main function like:

  public static void main(String[] args) throws Exception { }

and the outside class has same name with java file. But when I right click the java file, only have "run on server" or "run configuration" option, no "run as java application" like normally. Even if I copy a java file which I can run as java application from different project to this project (no error) then it do not have this option ??? I am quite sure it is nothing to do with the code. I checked the code, totally the same and should be able to run.(I am aware of similar questions on the website but I checked and my problem is not there)

So is there any extra steps I need to do with this project (like maven build ? -> I tried this but also can't run, how to run it properly ?) to have this option again ???

Really confusing !

user1314404
  • 1,253
  • 3
  • 22
  • 51

6 Answers6

7

If the project is already imported in your workspace, Right click on your and Configure > Convert to Maven... This will enable the Maven builder and force m2e configurators to setup all the required project natures on your project.

You can also install the m2eclipse-git connector to streamline the "import maven project from git" process. It should be available from Preferences > Maven > Discovery > Open Catalog.

Once it's done, you should import your project via Import...> Maven > Checkout Maven Projects from SCM

Fred Bricon
  • 5,369
  • 1
  • 31
  • 45
  • I tried all that but it doesn't solve the problem yet. If I run Maven buld, it is not successful, it has some errors. – user1314404 Aug 15 '13 at 06:04
  • 1
    Ok, I solved the problem! it was very close to your suggestion. Instead of check out by the first option like you said " import your project via Import...> Maven > Checkout Maven Projects from SCM", I choose the 2nd option which is "check out using existing Maven project", then choose the folder I imported using git previously. And now I can run the file from "run as java application". I will choose your question as my answer though. Thank you ! – user1314404 Aug 16 '13 at 06:29
2

Keep in mind that projects imported from GIT are not necessarily java projects, as Eclipse understands this term. Is the project, as it exists in the repository, an Eclipse project? If it is, it should have two files in its root folder named .classpath and .project. If these two files are missing, you will need to instruct Eclipse to create them, with logical defaults. Also, if the root folder of the project has a pom.xml file, then it is a maven project, which is a whole different kettle of fish.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • Yes, I have pom.xml file. What should I do to use it in Eclipse now ? I used to get project from Git and also have pom file but "after a while" I can run this (I don't remember exactly what step make it have option : run as java application). – user1314404 Aug 14 '13 at 14:08
2

I was facing a similar issue. Below steps solved the problem:

  1. Import project from git
  2. Right click on Project -> Import -> Import as existing Maven Project
  3. One maven module will be imported, you can open .java file with main method from that module
  4. Right click, run as -> you can see the option.
SilverNak
  • 3,283
  • 4
  • 28
  • 44
  • +1, I had a similiar problem for a maven project, I didn't pay close attention to the fact that the project I was dealing with is actually a multi module project. – prakashb Feb 20 '18 at 07:04
1

This can seem obvious but check if the function main is correctly declared. Function's signature must be :

static void main (String args[])

If you miss this point (ex: you forget scale declaration for args with []) you won't be able to use the option "run as appication", in my case option wasn't displayed.

websoft102030
  • 55
  • 1
  • 4
1

Sometimes it might happen when the maven project in not updated on importing to the workspace.

Right click on Project -> Maven -> Update Project..

Then Run As -> Java Application -> Select class containing main method.

0

If you are handling multi-module project, you might be trying to execute the java file( main class) within the parent project itself. That would be the mistake.

Make sure to open the sub- module ( would be listed as separate project in the explorer) and then execute the java file.

Vidhya28
  • 1
  • 2