eclipse "run as" not showing any option - java

I would like to run a java project by right clicking the file containing my Main method.
When I hover over the 'Run as' option I get an empty list. I can still use the "Run configurations" and get the project running.
I have the same problem (i.e no 'Run as' options) when trying to build the project using my Ant xml. Previously I would right click the build.xml file and select 'Ant build'. My workaround here is to add the builder into the project properties
I suspect I changed some definitions of the file type association, but don't know how to verify if this is indeed the problem. Any suggestions? Thanks

Main method is the entry point for the application to ru. Make sure your main method definition is correct. You may be having a main method, which is different from what JVM expects. Correct definition of main method should look like this:
public static void main(String args[])
Any change in this definition will make your main method to be a different method and JVM will not be able to find the entry point in your code to run.
Also your class containing the main method should be a public class and the corresponding file name should be same as that of class.

If your project folder is in another project, aka. belongs to a parent project, try:
Right click the project which main class belongs too, and click "import as new project" or something like that, then there should be a new project in the project Explorer at the bottom of the list.
Then right click the main class file in that project.
Because a folder is not a projcet, and run/debug as java application will only show up when it belongs to a project I suppose, but not sure, may this would help u:)

Related

How to create a executable jar with a particular class in a project in netbeans

This is my first question on stack overflow.
Please have a look at this first, please!!
I am creating an executable jar file by clicking on the button surrounded with grey color. The problem is that the exxcution of the application always starts from the "PersonalAssistant.java".
I want my application to begin its execution from "App.java" which also contains a main() method.
So, basically the jar file which I want should begin its execution from App.java class which has a main method.
I don't know how to change the path or execution class for jar.
Please help me!!!! I'm stuck with this silly problem. But I want a solution for this.
I'm using Windows10 with Netbeans 8.2 IDE
You should be able to reconfigure your IDE and rebuild the executable jar specifying which class you want to use as the executable.
in your IDE add java -cp myjar.jar App
to your compile command
this might also help
Look at the picture above. Right-click on your project and select Properties. Select Run. Look for the Application Class field. In my example, I have javafxapplication211.Main. This structure is projectName.MainClass. You should just need to change the MainClass part to the class you want to start your project. For example, if Main.java was starting my app and I wanted to change it to App.java. I would change javafxapplication211.Main to javafxapplication211.App.
If you hit Browse, it will give you a list of available classes that can start the application.

the main class is not found to create executable jar

I am trying to create executable jar out of a java project. the java project has the main method in a java file named MainTest.java.
To create the executable jar, I did the following:
right click on the project->export->java->runnable jar->next button
Then I get a window with a list asking me to specify the launch configuration, the problem is , despit the java project has a main method in the file namedMainTest.java, this file is not listed in the list
how to solve the problem or how make the java file that contains the main method listed among the others in the list.
Note: the namedMainTest.java has the main method but it is empty "does nothing"
update
I am using Eclipse.
and please have a look at the screen shot below. on the left , is the project with the java file that contains the main class "MainTest.java" and on the right, a list of classes and the "MainTest.java" is not among them instead there is another class names "TestMatrix.java" and i do not know why it is there
I'm assuming you are using eclipse?
Your desired main function appears if you ran your program before it. The dialog tells you to select a "run configuration". Since your main has not been run yet it does not appear.
Another way to generate an executable jar is by going this way:
Export>Java>JAR file>
At this dialog select the resources to be exported (your project e.g.) and specify
the name and destination of your jar file.
After that continue (2x next) to the "JAR Manifest Specification" screen.
The last option asks you to specify your Main class. Press finish after configuring your project and you are done.

Unable to run Java code with Intellij IDEA

I have just downloaded the IDE, and I want to edit my first Java file with it, I'm not interested in creating a whole project, just editing the single file.
So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.
I write some code and the main run and debug buttons are greyed out! I can't run my code!
I have already installed Java 8 update 45 64-bit (I have a 64 bit OS) as well as the Java development kit (J8U45). I have set my global IDE SDK as my JDK installation, and when it prompts me I also set this as my project SDK, but still the run and debug buttons are unable to be used!
Edit: I am also unable to run my file regardless of if its in a project or not.
Edit 2: Screenshot of my project setup
Move your code inside of the src folder. Once it's there, it'll be compiled on-the-fly every time it's saved.
IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.
Also - while I can't see all of your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a main method (specifically public static void main(String[] args)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).
My classes contained a main() method yet I was unable to see the Run option. That option was enabled once I marked a folder containing my class files as a source folder:
Right click the folder containing your source
Select Mark Directory as → Test Source Root
Some of the classes in my folder don't have a main() method, but I still see a Run option for those.
right click on the "SRC folder", select "Mark directory as:, select "Resource Root".
Then Edit the run configuration. select Run, run, edit configuration, with the plus button add an application configuration, give it a name (could be any name), and in the main class write down the full name of the main java class for example, com.example.java.MaxValues.
you might also need to check file, project structure, project settings-project, give it a folder for the compiler output, preferably a separate folder, under the java folder,
Don't forget the String[] args in your main method. Otherwise, there's no option to run your program:
public static void main(String[] args) {
}
I had the similar issue and solved it by doing the below step.
Go to "Run" menu and "Edit configuration"
Click on add(+) icon and select Application from the list.
In configuration name your Main class: name of your main class.
Working Directory : It should point till the src folder of your project. C:\Users\name\Work\ProjectName\src
This is where I had issue and after correcting this, I could see the run option for that class.
Something else that worked for me:
Right click the folder in src containing your main
You'll see an option "run 'file.main()'" with the run icon.
Click it, and then the run icon in the top right and bottom left will turn green from then on.
Sometimes, patience is key.
I had the same problem with a java project with big node_modules / .m2 directories.
The indexing was very long so I paused it and it prevented me from using Run Configurations.
So I waited for the indexing to finish and only then I was able to run my main class.
If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:
Click on add icon and select Application from the list.
In configuration name your Main class: as your main class name.
Set working directory to your project directory.
Others: leave them default and click on apply.
Now you can run your program.enter image description here
Last resort option when nothing else seems to work: close and reopen IntelliJ.
My issue was with reverting a Git commit, which happened to change the Java SDK configured for the Project to a no longer installed version of the JDK. But fixing that back still didn't allow me to run the program. Restarting IntelliJ fixed this
-First Move Your Code Files in side the "src" Folder
-Make sure your Main method is declared like the following
public class Main {
public static void main(String []args){
}
}
then:
Go to Project configurations
select Java application,
check allow parallel run
and select your main class
and it should work
If you are just opened a new java project then create a new folder src/ in the man project location.
Then cut and paste all your package in that folder.
Then Right click on src directory and select option Mark Directory As > Sources Root.
If you use Maven, you must to declare your source and test folder in project directory.
For these, click F4 on Intellij Idea and Open Project Structure. Select your project name on the left panel and Mark As your "Source" and "Test" directory.

Use Java class file in IDEA

Is it possible to use a class from a .class file in IDEA? For example, if I have a directory with two files Ball.java and DemoBall.class, how would I use DemoBall in Ball? It says it cannot find symbol when compiling. I just want to tell IDEA that there is a compiled Java class in the directory and to recognize that, I don't want to decompile it.
EDIT:
Open IntelliJIDEA. Now Press CTL + Shift + ALT + S. This will open Project Structure.
Now Select Module from Project Settings (left hand side). And Dependencies tab from the right hand side.
Now Click on the + on the extreme right hand side toolbar. Select Jars or Directories. A new Window, Attack File or Directories will pop up.
Now browse to the location, where DemoBall.class is located. Click OK.
Select the CheckBox under Export against the location just selected. Click OK on the main Project Structure window. You are done.
Original(This one it seems is not the right way):
Open IntelliJDEA. Create a Project, with simple Java Class named Ball.java
inside it, having one main method(empty body).
Run this class, so as to create one out folder.
Copy DemoBall.class. Now through Windows Explorer/File System, go to out folder and paste DemoBall.class inside out\production\ProjectName folder.
Now simply write the code that uses this DemoBall class, inside the main method of Ball.java class.
Previously, I was wondering, as to why the red lines are coming still, even though, the program is running fine. But once, I restart, IntelliJIDEA, everythingy works fine, without showing any errors.

Import project to another workspace including run configurations

I'm moving a bunch of projects from one workspace to another.
Each of the projects had a main() function, which was called with some default arguments that was part of the Run configuration for that project.
When I import these projects, it looks like these run configurations are not copied. I need to specify the input arguments etc again - Run as -> Run Configurations -> update stuff .
Is there a way to copy them over also?
There may be an easier way, but if you find all the file named *.launch - that will give you the list, then just copy those files. We actually save the base set of launch files we use as part of our project so that they are part of the project. For launches created on-the-fly, you will find those in the workspace.
Open the menu "Run -> Run configurations ..." and select the Launch configuration you want to share inside your project. Goto the common tab inside the selected launch and there you can specify a "Save as" location via "Shared file:". As location select your project. So then an appropriate ".launch" file is created and can be checked in or exported with the project itself.

Categories