New to Netbeans, trouble running multiple .java in same .pkg - java

So I recently started studying IT and currently we are working the basics of Java and using Netbeans 8.0.2 to do so.
I have several homework assignments that I enjoy doing a lot but I have got a question about the program.
I did the first assignment and it ran without any errors and showed me the output I was expecting, so I go on to make another .java mainclass file for my second assignment, within the same .pkg as my first assignment. But now every time I press the run button it only shows the first assignment in the output even though both are stated as mainclass files.
I know my code is correct because upon making a new project and doing the second assignment in a totally different project, it runs no problem and shows only the output of the second assignment.
Is there something I'm doing wrong? Because it would be kind of annoying to create a new project for every assignment considering I'm doing 10+ a week.
Any help is greatly appreciated!

You must change the main class to execute in:
File > Project Properties > Run > Main Class
One example is available in this article:
https://www.thoughtco.com/using-multiple-main-classes-2034250

Related

Why does sts ask me to choose the select type when they run the project?

When sts executes the project, ask me what the select type is. I didn't ask for another project.
The sts always ask me every time they run, is there a way not to make this pop up? What did I do wrong? The class name is related to my work, so I covered it. Sorry
enter image description here
I solved it! There was a main function among the classes you asked to choose. There was something I made during the class test in the beginning, and I guess it was asking about the application.java file and what you want to run It works right away when I remove the main function of another file!

Java noob, wondering if I made a mistake regarding packages

So to give context, I am new to Java and have no other programming experience. The IDE I am using is NetBeans. I picked up a book called "Sams Teach Yourself Java" and the tutorials in this book are having me put all the different classes I write in the same package. The problem with this is that when I want to run a class with attached arguments I can't just run the file from the "Run" tab. The book tells me to run it as a main project. But if I select run as main, one of my other classes runs. I figured out a work-around buy setting a main class, but I haven't found anything online about this and want to make sure I am not doing something stupid. This is my first question and any tips on how to ask effective questions on this site are appreciated.
While learning, there is nothing wrong with putting many classes in the same package. The reasons for separating classes into different packages can wait until later lessons and learning.
You can run a particular class as a Java application in Netbeans, assuming it has the correct main() method, by pressing ctrl-shift-f5 while that class is the one currently selected (i.e., currently displayed in the editor pane). You can run the most recently run java application by pressing ctrl-f5, even if that class is not the one currently selected.
This question is clear enough. If you get to a programming problem, go far enough to have tried something that doesn't work as expected. To ask about it, try to show the smallest program that illustrates your problem; tell WHAT is happening that you do not expect (or not happening that you do). If there is an error message, include all of it, don't just describe it.
Is each class a new project/example from the book?
You could have multiple classes within the same package that each have a main method. Only the main method within the class that is selected to run/passed on will be called.
In NetBeans, there is a little drop down arrow that you can press and it should allow you to specify which one to call.
The application is running properly. That I can assure you. Since all the classes you created contains main method the compiler is confused because it does not know which class's main method should be executed successfully. Hence, it builds all the methods but does not execute them. If you observe the output tab, after clicking on the run button, it will always show build successful. This means it it building the classes but not executing them. To execute each class separately, either right-click on the class and then select the run option, or use the keyboard shortcut 'Shift+F6'. This shortcut executes the class you are currently working in.

Create and Run a simple java Program in intelliJ without all the advanced features?

I am just learning java and have come to a point where coding in 'vi' is killing my hands
I'm considering an IDE like 'intelliJ' with code Completion but don't want to use all that project tree , packages etc and be caught up in its complexity, i'll learn how to use an IDE later ,
Can someone tell me how to just create and run , just a .java file along with codee completiona and debuggin , so that i dont need to save with 'wq' , compile and open vi.filename.java each time, this is why i want an IDE , to make this easier, so please guide me on how to do just this in IntelliJ or suggest some other IDE with good codeCompletion,
I know this question is subjective but i;m askinng it since i just had a hard time typing a simple GUI quiz Application
I would just create new Java project (File/New Project/Java), select Create project from template and choose Command line app. This creates a project containing a class with empty main method, which you can run by hitting SHIFT + F10 or debug using SHIFT + F9.
This whole process takes only like 20 seconds.
And as far as the directory structure complexity goes, you have just one directory that you need to be concerned about - src/. You can keep all your classes in the default package if you want and therefore all your code will be in src/ without any subdirectories for packages.
I don't think it gets any easier than this.
Most IDEs make you create a project to start coding, so I usually have one project called 'test' for whenever I want to try a bit of code. Make one java class file that has a main class and use F5 or find the run option in the menu.

Run main method of a class in the test folder

Tried to google but got hundreds of unrelated issues regarding testing. I guess I'm missing a crucial keyword to reduce the number of hits to something that is relevant for me.
I have a class in src/test-integration/java which i need to run, since it is a tool for extracting test data from an database. It's basically just a little script in the main method.
However when I try to "run as java application" in Eclipse it says: Error: Could not find or load main class x.y.z.MyClass
I know it has worked before, but not sure how I got it to work.
Sorry for any missing information, please feel free to ask for more.
Any ideas of what I'm missing?
Added the whole full path to the java class in the Java Build Path properties in Eclipse and deselected "allow output for source folders" and selected it once again (don't know if that did something, but I include it anyway).

Netbeans problems with reflection

Why Netbeans takes so much time in java.lang.Class.getDeclaredField(String)?
I have isolated my problem and I have two projects:
I created a Netbeans Platform program with one module that has one window and one button in it. So when I click it, program runs the function that I need.
I created a form that will call that function.
So now I have two programs that are the same and run from an AWT thread. The key difference is that one runs under Netbeans Platform structure and the other one is alone.
Profiling the two programs I found that the big problem is when it calls "java.lang.Class.getDeclaredField(String)" Netbeans program takes 30,784 ms and Swing program takes 2,055 ms
It seems to me that Netbeans has some sort of security that checks when somebody is trying to access a class through reflection.
Does anybody knows what is it? and how to turn it off??
Thanks
HS

Categories