This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Eclipse .java file without project and package creation

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
GB
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Welcome to JavaRanch!

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.
 
Saloon Keeper
Posts: 28322
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

As an example of HOW important, I can point you to a problem with Internet Explorer 9 and JavaScript. Microsoft added a new functionality for AJAX in IE9, and they didn't bother to check for compatibility with existing solutions. Since JavaScript isn't big on namespaces (package naming), it collided with a large base of existing applications. I'm currently running one of my main systems with a hack applied to make IE9 pretend it's IE8 as a result.

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).
 
John G Wright
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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 ?

Thanks



 
Tim Holloway
Saloon Keeper
Posts: 28322
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks

 
Tim Holloway
Saloon Keeper
Posts: 28322
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John G Wright wrote:
When I click the small arrow beside Run (play button ), I cannot find options for Compile. -- > Ctrl F11 is for compile and Run.



That is because Eclipse compiles the file automatically when you save changes to it.
 
John G Wright
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"That is because Eclipse compiles the file automatically when you save changes to it."

Yes, I am able to see the keyword typing errors. Compilation errors getting reflected after I press ctrl +F11. Thanks for the reply.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you dislike Eclipse's autocompile as much as I do, you can turn it off on the Windows > Preferences > General > Workspace page.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic