r/learnjava • • Dec 29 '21

How to write a program?

I guess it sounds strange because as I try to write it down to ask the question it sounds strange to ME... I landed a job that requires me to know java, but not, how to write a java program. And (can you say "Unicorn"?) they are teaching me Java on the job via a Udemy course and some hands-on instruction from a couple of the dev team.

I am hip-deep into this Udemy course and learning Java at a furious pace. I have learned many concepts and constructs and syntax... but not the basic building blocks of a program.

Would one (or more) of you either point me to a resource or explain - not the code - but the pieces of a program. Packages, classes, wtf is a jar?, how do these (and whatever I am missing) stack together to become an actual, fully realized Java Program?

In an exercise, I wrote a switch statement that takes a number, 1-12, as input and outputs the corresponding month. I'd like to make that exercise into a portable program and let my daughter fart around with it to learn that information, but I realized that after six weeks of grinding this course, I don't know how!

Before you ask... I ask here because I the dev guys are hellishly busy - which is why I have a job, I need to get some very tedious, customer-facing, sometimes Java-related scut work off their plate - and I don't want to bother them with personal curiosities that are outside the scope of my job duties.

https://lensdump.com/i/rjkPzZ

Here is an exercise I downloaded - I think from my UDemy class, it may be from another source.

The top level, "Project", is the container for everything else, that I get.

  • ExampleForLoop

  • External Libraries

  • Scratches and Consoles

are what...packages? (Scratches and Consoles / scratch.java doesn't have anything to do with anything, it's a snippet from another lesson about vectors...unless this was a lesson about iterating through a vector...)

  • .idea

  • out

baffle me...what are they, do I need them, do I create them or does the IDE do it for me?

  • src

seems to be the meat of the deal. Is LoopExamp.java what happens after classes

  • LoopExamp

  • Main

  • Student

are compiled?

What is ExampleForLoop.iml?

54 Upvotes

15 comments sorted by

28

u/Jezoreczek Dec 29 '21

I have learned many concepts and constructs and syntax... but not the basic building blocks of a program.

Normally there is no need to construct a Java program from scratch. It's a good practice to do it with Maven or Gradle instead.

Packages, classes

These are just means of organizing your code. Think of a class as a single piece of a bigger puzzle. Package name and class name specify the location of the piece.

wtf is a jar?

JAR file is just a zip. Seriously, take 7zip and unzip it. It contains all compiled .class files, which store the bytecode representation of your classes. When running java -jar ..., the JVM will first look for META-INF/MANIFEST.MF file inside the JAR - this file tells JVM which class should be the entry point of your program (which class contains the main static method).

how do these (and whatever I am missing) stack together to become an actual, fully realized Java Program?

JVM will load the jar (unzip it), then load the classes and run your main method. There are more things happening there of course, but that's the gist of it.

I'd like to make that exercise into a portable program

You can build a "fat jar" with Gradle. It will include all libraries and such. Doing it all manually is a pain in the ass, and there is really no good reason to do so.

Before you ask... I ask here, because

You don't need to explain yourself, wanting to learn Java is good enough reason to post here!

10

u/Tigloki Dec 30 '21

Thanks very much! This was very instructive.

7

u/khooke Dec 29 '21

The official Java tutorials are a good place to start to answer your questions about the basics, what is a package, what is a class etc?

https://docs.oracle.com/javase/tutorial/

Re. not wanting to ask questions, most devs will welcome questions from another dev just starting out. Never be reluctant to ask questions, respect their time though, you could ask 'hey, when you have a few minutes sometime this week, could you spend 15 mins with me to explain xyz?'

The benefit of asking questions of the experienced devs you're working with is they'll have valuable lessons learned that they had to learn the hard way first hand, things you'll never get from a book or tutorial. They'll also have plenty of 'tribal knowledge' that's specific to your company/project that you won't be able to learn anywhere else.

2

u/Tigloki Dec 30 '21

They'll also have plenty of 'tribal knowledge' that's specific to your company/project that you won't be able to learn anywhere else.

This is a great point. You made several! And thanks for the link!

26

u/Skiamakhos Dec 29 '21

I'd say grab a copy of "Java in a Nutshell" by Ben Evans and David Flanagan. In the early chapters it tells you all about JARs and the way you compile and execute Java programs. It's also a pretty good desktop reference to the core APIs. The latest version covers up to Java 11 so you do get the essentials about how to deal with modules.

14

u/Targaryen_99 Dec 29 '21

If you just headballed into syntax and all without knowing how everything connects at the basic level, maybe head first java would be a good start. It tries to connect every basic thing. After that JAVA in a nutshell might be good.

8

u/mrfroggyman Dec 29 '21

Well as far as I can tell, a jar is an executable for your java program.

So you compile your java program, create a manifest which states which of your classes is your main class, and execute some command to create a Jar file using that manifest, and then you can execute that jar like a regular .exe through a command.

There may be better answers and better ways to do this tho

1

u/SlapdashSlamdance Dec 29 '21

I think the question is a bit open-ended. It depends on what you mean by a Program. This can be a bit of a leap conceptually, because you learn Java typically in an IDE where you get to see the output of your code and it seems like it's something completely different than the software that you download and run.

edit: whoops, accidentally pasted the same link twice.

1

u/Tigloki Dec 30 '21

This is almost exactly what I was trying to ask for. I find that I am so ignorant that even articulating an intelligent question can be difficult. I edited my original question to include this information:

------------------------

https://lensdump.com/i/rjkPzZ

Here is an exercise I downloaded - I think from my UDemy class, it may be from another source.

The top level, "Project", is the container for everything else, that I get.

ExampleForLoop

External Libraries

Scratches and Consoles

are what...packages? (Scratches and Consoles / scratch.java doesn't have anything to do with anything, it's a snippet from another lesson about vectors...unless this was a lesson about iterating through a vector...)

.idea

out

baffle me...what are they, do I need them, do I create them or does the IDE do it for me?

src

seems to be the meat of the deal. Is LoopExamp.java what happens after classes

LoopExamp

Main

Student

are compiled?

What is ExampleForLoop.iml?

2

u/GiveMeSopas Dec 30 '21

You can try Chad Darby's Spring and Hibernate for Beginners. It's beginner friendly and will teach you stuff that's used IRL

2

u/Powerful-Reason Jan 08 '22

Lots of good comments. I took java in college and thought that they did a poor job of practicality with teaching Java as a professional. There was not a lot about packages, making executables etc. I learned that basically the same way you are now, asking questions. The .idea is for IntelliJ, I believe. Packages are like basically code that has been packaged together. Like if you have a dresser with multiple drawers, you might put socks in the top drawer. Therfore you could say that socks belong to top Drawer. And socks could be topDrawer.socks. This could then be something like dresser.topDrawer.socks haha. Jar is just a zip file, as someone already pointed out. Some jars can be executed, and some can just be a collection of Java packages, classes or whatever. I write some Java code that gets packaged in jar files, then the jar files get imported into a larger framework and executed. So my jar file doesn't contain a main() anywhere, really. Just libraries that are called and referenced in the framework. I'm not real knowledgeable on a lot of the ide stuff, or tools etc. I do use gradle and intelliJ, but that's because there is an existing tool chain built for the framework, that has some code generation mechanisms.

3

u/johnmc325 Dec 29 '21

First off you don't mention the Java version you are using. Java 8 is easier to get started with in my opinion but sooner or later you will have to embrace modules.

Take a look at this, it might help p. Java 11 module based if I recall. You only need go as far as the jar to move to another machine so long as it has the same version of Java or a compatible version.

https://softwarepulse.co.uk/blog/javafx-11-desktop-msi-application/

As if you have any problems. Good luck.

3

u/[deleted] Dec 29 '21

You don't really need to worry about modules. As long as you're using relatively new dependency versions then you can code to your heart's content and never ever deal with modules.

1

u/vladadj Dec 29 '21

I guess that course you're taking sucks, if you haven't learned that do far.

In your place, I would go back to plain text editor and command line. Forget IDE, build tools, or what ever you used so far. Just use Notepad++ or what ever has syntax highlighting that is all you need.

Now write a simple class with nain() method. Compile it from command line using javac. See what you get as output. Now try to run that using Java command.

Next step, write a little more complex program which contains of multiple classes. Compile it and run.

After that, package you compiled classes using jar command. Create a manifest for your archive. Now run your jar.

After this exercise, it should be more clear how Java programs work.

-8

u/[deleted] Dec 29 '21

Algorithm + Data Structure = Program