Eclipse .java file without project and package creation
John G Wright
Joined: Oct 25, 2012
I am beginner in Java and new to Eclipse. I initially used Edit Plus and I feel it very convenient to create .java file and compile it in the command prompt. I recently started using Eclipse but not able to create .java files without project and packages. As I am a beginner , I want to try lot of basic programs to understand the concepts for which packages and project not required.
1 ) Can .java files can be created and compiled in the Eclipse without project and package creation as I do it Edit plus ?
2 ) Also, when I tried creating a new project and want to put all the files that into that project. I right clicked the project and click import and selected the files. Now I have many .java files under my project same. I removed Exlcude buildpath for each .java file and tried compiling it. But it shows error in the main line. Though all are simple programs using Println, but I am not able to rectify the errors.
Thanks & Regards,
author and iconoclast
Eclipse essentially does require you to create a project and put your Java source code into it. It's actually possible to just use File | Open... to open a Java file and edit it, but the editor will have reduced functionality; it will act more like a plain text editor and won't be able to help you write code and find errors the way the full editor can.
As to your second question, I can tell you that when you create a project, it should be a "Java Project", and when you import Java source, it should be into the designated "source folder" (named "src" by default). I don't know what you mean about "exclude build path", but it doesn't sound right to me. Following the simple steps above should set things up so that simple standalone Java programs compile perfectly.
Here is a nice "getting started" video that should help you get your first project running.
An Eclipse project is nothing more than the work directory that helps organize one project and its operating rules. If you want to just mess around with random classes, make a "shoebox" project and dump them all in there. You can setup debug/run profiles for the different classes.
You really should get in the habit of packaging. ALL Java classes belong to some package or other, and if you don't include a package declaration, they go into the default package. The default package is not a good place to put things. For one thing, a lot of Java systems get really annoyed with default package objects. Packages provide unique namespaces so that different classes with the same names won't collide, and that's very important with large Java projects that pull in a lot of third-party libraries.
Your can name packages whatever you want (subject to syntax restrictions). Conventionally, they should not contain upper-case letters, and punctuation should be avoided. The general convention is to use the domain name of your organization (employer or school), so for example, "edu.ucf.cop1170.timh" would be a good package name root for me (actually, I use com.mousetech, since that's my company).
Customer surveys are for companies who didn't pay proper attention to begin with.
John G Wright
Joined: Oct 25, 2012
@Ernest - Thanks for the reply and also for providing the video link. Yes it worked ( It is a part of my mistake as I didn't include the package name, assuming that eclipse will take default package ) . Do we have a keyboard shortcut for Java Project ? ( and also for Java Package and Classes )
@ Tim - Thanks for such a detailed explanation and making me to understand the importance of package name even if it is going to be a small program.
Your answers made me to think about two options -
Though my files by default is in src folder, Is there is any option to put .java files in different folder ? Can we direct the .class output file resulting in compilation to be put in different folder instead of bin ? ( As you know we can do this in the command prompt by having -d option ). How to do the same in Eclipse ?
I pressed ctrl + F11 and it got compiled and run and got the output. But I want to compile alone and run it in the command prompt. Is there any way to get around it ?
I created Toggle breakpoint and able to see the output step by step. But not able to find the flow of program ie..If I create a toggle break point and compile and run, it stops at the toggle break point and giving the output of that particular line. I want to trace the program as something we have in C/C++ - trace, where it will trace the flow of program. Can the same be done in eclipse ?
Yes, Eclipse has the ability to designate a folder in an Eclipse Java project to receive compiled classes. By default, it's named "bin". Look at the project properties Java Build panels. You can designate alternatives, however. In Maven projects, the place to put compiled program classes is in the target/classes directory. Test files go in the target/test-classes directory. You do not have to "push a button" to compile in Eclipse. Eclipse will automatically recompile a class when you save edits to it.
It's a very good idea to always put your build products in a separate directory from the source directory. I learned to my pain a long time ago that mixing inputs and outputs can cause trouble.
When you designate a classes directory for an Eclipe Java project, it will automatically be added to the classpath of any run/debug profiles you create for executable classes in that project.
To run an Eclipse app from the command line, do it in the ordinary way. Use "-classpath PATH_TO/bin" to let the JVM know where the classes are.
You can trace Java extensively. The debuggers is actually part of the JVM, and Eclipse just overloads a GUI onto it. F6 will do a step-over, F5 will do a step-into, F7 will do a method return, and F8 will do a "GO". Which doesn't match the more common conventions of other debuggers such as F5=GO, but it still works pretty much the same.
John G Wright
Joined: Oct 25, 2012
Thanks a lot once again and creating awareness about debug Function Keys which I am totally unaware off. As eclipse got lot of windows, so exploring bit by bit. I tried Ant in command prompt ( with Edit plus ) but yet to use it in Eclipse. Also thanks for giving a brief intro about Maven ( will be learning at least the basics of Maven )
"When you designate a classes directory for an Eclipe Java project, it will automatically be added to the classpath of any run/debug profiles you create for executable classes in that project.
To run an Eclipse app from the command line, do it in the ordinary way. Use "-classpath PATH_TO/bin" to let the JVM know where the classes are. "
When I click the small arrow beside Run (play button ), I cannot find options for Compile. -- > Ctrl F11 is for compile and Run.
Yes, I tried in the command prompt both compile and Run and got the output.
Yesterday I used Indigo and today I am using Juno and I really liked Juno compared to Indigo.