I can't run the java.file that I want - java

I'm a beginner at Java and Eclipse and I will have an exam this Thursday.
I just imported those .java files on that package and project I created myself (I named the package java_10 just as it was needed, I'm mentioning this because I don't know if this is going to be useful information or not).
The problem I have is that when I click run in Eclipse, it doesn't run the current opened file but it runs another file, like in this case the KalkulimiB.java (as you can see, the result that is showing down on the console "z=15" is definitely not coming from Bank.java).
It didn't work at all in the beginning when I imported the files so I messed up a little with the run configurations so here's the picture how it looks like at the moment. Please zoom it up as it's needed, Please don't flag the question as a repost or something, I really searched for this problem but the solutions just didn't work or they just didn't really ask the same exact question as I am.

Launch configurations are a bit with a mind of their own until you know Eclipse better.
You can always explicitly run a specific class by right-clicking it in the Navigator and choose "Run" (or "Debug").

The files which can be run are those with
public static void main(Strings[] args){
//code to be run
}
other classes cannot be run but their methods can be run inside the main function of another file. Be careful though, if there are multiple main files open then the compiler will almost always run the main file that was run last.

Related

My IntelliJ Project Creation isn't providing me with any structure

In the past 24 hours, I've started having an issue with my IntelliJ not properly structuring projects for me when I create them from a template as a basic java project. I'm not sure why it's doing this, but as you can see in the image, there is absolutely no structure in my project, no src folder or anything. The path is correct on the header bar of the application, but nothing shows in the actual project view.
I thought it might be an issue with the project waiting to load, but I've waited 20+ minutes and nothing seems to change.
Also, if I try to edit the code in the Main class, I get a non-project file protection pop-up, and if I try to run it, I get an Edit Configuration pop-up.
Does anyone know how to fix this and make it work like it should?

eclipse Luna + Java : 2 main files, compilation not proper

Having strange problem for couple of days.
I have written 2 main files, so to test based on need.
I have been facing following strange problems,
The main fail stops executing after some statement. doesnt hang! after cleaning the build, and compiling again it got executing!
I updated few System.out.println statements, but i am cant see new statements.
I excluded the unwanted main files and tried. even commented the whole unwanted main files and tried.
I even tried changing the main file name that i am using.
I observe the eclipse is not compiling the main file i wanted or it is taking old class file some where residing?
I cant see the unwanted class anywhere in the workspace!!
anyone experienced similar problem?
Please try right-mouse click on the class in "Package Explorer", then
form the menu choose "Run As" -> "Java Application".
This will run (and compile if needed) the selected source code.
I copied a main file from other project, and that package got retained in that file. when i modify the code in copied main file, forgot to modify a constructor name which was pointing to earlier package. I missed to trace import statements and it was not expanded. when i try to copy the file to post in stackflow, i found earlier package was included and found that was the culprit!!

NetBeans (Java) - loading additional .java files

So I had an assignment to write a program for the game Othello/Reversi. I used NetBeans. Now the teacher sent us a "ReversiGUI.java" file that uses my own ReversiPlay.java and loads it onto a GUI so that I can test the game with my bots etc.
If I use the regular javac ReversiGUI.java and then run java ReversiGUI this works very well. However, it would be much easier for me to run the GUI from NetBeans, because I am getting some errors in the GUI, and I want to fix my code, but the info from the command line is very minimal (type of exception and line number). In NetBeans I will be able to debug and see which specific values are causing the problem.
I tried loading the GUI file into my ReversiPlay project, but every time the line ReversiPlay.someMethod is used, NetBeans says it does not recognize the symbol. So I'm guessing there's a specific way of loading the ReversiGUI.java file in NetBeans but I have no idea how to do it..
Any ideas?
Your problem is that NetBeans expects a package declaration, unlike command line. At the top of both your files put the following line:
package reversiplay;
That should fix your errors. Hope this helps!

Could not find or load main class Swing Java

Error: Could not find or load main class jewelleryerpapplication.GUI.ERPMainMenu
Java Result: 1
Actually i am using Net Beans 7.1 and in that i am continuosly getting the same error but un able to get the solution for that even after installing the latest update for that. The solution which i got from some body that create new project and copy the source and library foldes in that Project from the previous which generate the above error. this solution works for me but i need some simpler solution for this problem.Lot of thanks in advance.
Don't Forget To Give UpVote It If It Helps.
Right click on project node, go to Set configuration, select the main class for your application. Then clean and build.
Even if this doesn't solve your problem, then delete the Netbeans cache by deleting the (index) folder
User\.netbeans\6.9\var\cache\index\
I think that the following is happening:
If this is a third party application, you have not included some jar files on your class path;
Or, (which in my opinion is the most likely) you do not have a method which has this signature: public static void main(String[] args). This method defines the main entry point for your application, not having it might cause that error when you try and run the project. To my knowledge, Netbeans creates a main class with such a method automatically whenever you create a new project. This might be the reason why you are not having this exception when you create a new Project and throw everything in it.
You might be having an issue with Netbeans itself. You can start by either checkin gout this previous SO thread in which a similar (I think) issue is being discussed, or else, as a final resort, you might want to remove Netbeans and all its files, and install an earlier version. You can also try and see if you get the same issue if you use a different IDE such as Eclipse.
the solution is,right click your package in netbeans,go to properties,source now go to the jre option and choose your version,the programme will now run.

Java Reflection not working on my system - working for team members

I am working on a team project in Java. One requirement is that we dynamically populate a drop-down menu of all classes that implement a certain interface. New classes can be added after compile time. To accomplish this we are using reflection.
Problem: All of the drop-down menus are blank on my system. I cannot for the life of me figure out why they are not populating. All other 5 team members have it working on their system.
Things I tired that didn't work:
1) Installing most recent eclipse (galileo) because rest team was using it
2) Re-install most recent java release (jdk1.6.0-17 and jre6)
3) Check PATH and JAVA_HOME variables
Any thoughts as to what else I can try or if something I did should have solved it and didn't? It is driving me crazy.
Edit:
I should have been clearer that we are developing in a team. We are using SVN for version control and we are all running the exact same source code. I even tried checking out a fresh copy of the entire tree from SVN, but I had the same issue with reflection on my system while it worked for teammates.
The team created an executable jar and that ran on everyone's system fine except for mine. Everything worked for me except the reflection bit.
You need to debug your application. This means you have to systematically explore possible causes of the problem. Here are some things that come to mind:
Could your GUI be failing rather than reflection? What if you output with System.out.println() rather than your menu?
Is your reflection code throwing an exception, and are you ignoring it?
Is your reflection code actually being called? Toss a println() in there to be sure!
Is the test for the interface suffering from a typo or similar error that's causing it to fail? Try finding classes that implement Serializable instead!
Is your reflection test running in the main thread and trying to update your GUI? You need to use SwingUtilities.invokeAndWait to get an update to the Swing worker thread.
You're working with Eclipse; Eclipse has a fantastic debugger. Set a breakpoint near where your main action is and then single step through the code.
PATH and JAVA_HOME won't help. PATH only affects dynamically-linked libraries ("native code"). JAVA_HOME is a scripting variable that happens to be used by some Java-based utilities like Ant and Tomcat; it means nothing to the Java runtime itself.
You need to be investigating the classpath, which should be specified by the -classpath option to the java command, in the Build Path in your Eclipse project properties, or in the Class-Path attribute of the main section of a JAR file if you're launching java with the -jar option.
From within your code, you should be able to list the contents of your classpath by examining the system property, "java.class.path"
System.out.println(System.getProperty("java.class.path"));
Problem solution:
Classpath leading to source code must have no spaces in it.
I am running windows XP and, for whatever reason, if the classpath that leads to the jar file or source code that is using reflection has any spaces in it, then the reflection fails.
I took the jar file that works for the rest of my team and ran it from C:\ on my system and the reflection worked perfectly fine.
I do not know why this is so please comment if you know what is happening.
Might be a long shot, but look for differences in security settings for you and your team mates. Article describing more details http://www.ibm.com/developerworks/library/j-dyn0603/ heading "Security and reflection"

Categories