0

I'm an old hand at Unix and C++ but not RPM or Java. I have to write a Java program. If I get a Hello World running, I can use the internet to find my way, but how can I get a Java Hello World running?

I do have root on a machine, but don't know much about the machine's provenance. Using SuSE 11.4, as seen with cat /etc/*-release.

halfer
  • 19,824
  • 17
  • 99
  • 186
Swiss Frank
  • 1,985
  • 15
  • 33

1 Answers1

2

You need, currently, it seems, Java 1.8. You need the development kit "JDK" not the runtime environment "JRE."

If you have it you'll see the jdk1.8blahblah shown here. You'd think they'd put Java in the package name, and yes, they used to. But no more, so grep'ing for java will only find old versions.

rpm -qa |grep jdk
jdk1.8-1.8.0_191-fcs.x86_64
java-1_6_0-openjdk-plugin-1.6.0.0_b20.1.9.7-1.2.1.x86_64
java-1_6_0-openjdk-1.6.0.0_b20.1.9.7-1.2.1.x86_64

If not, java.com is NOT the answer--they have only the JRE it seems. Their search tool doesn't return the link you need in the top ten results with "JDK" or "download JDK 8". But Google finds it on the first hit, currently: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Get the x64 version for 64-bit, x86 version for 32-bit.

Firefox has a built-in installer for RPMs. If you let it install, you'll get a dialog called Downloads (informational only; close); Installation Summary (hit Finish). You'll be prompted for root password, which I don't find comforting. Or, you can just save the RPM file anywhere, then run as root (substituting your file name)

rpm -ivh wherever/jre-8u191-linux-x64.rpm

As it runs, it will output (among other things) the package name. For further rpm commands, you use this package name, not the file name. (If you try rpm -q filename, or rpm -e filename, it tells you file name isn't installed, which is confusing if rpm -ivh seemed to just tell you it was installed!)

Now where did it install the tools you need?

> rpm -ql jdk1.8-1.8.0_191-fcs.x86_64 | grep 'bin/java$'
/usr/java/jdk1.8.0_191-amd64/bin/java
/usr/java/jdk1.8.0_191-amd64/jre/bin/java

If you see them there you can delete the RPM file.

OK, it's in /usr/java/jdk1.8.0_191-amd64 . Add /usr/java/jdk1.8.0_191-amd64/bin to your PATH in your .cshrc or what have you and re-source it.

> java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
> javac -version
javac 1.8.0_191

Great, we see 1.8!

Java keeps source code in .java files, one file per class. Those are read by the compiler, which outputs bytecode in .class files. Those then can be run.

Put this in a file called HelloWorld.java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

In that directory, type the following. They should succeed without further output.

> javac HelloWorld.java
> java HelloWorld

You should then see the Hello, World output.

The above may need a -cp . or -cp ./ option.

As to bootstrapping up the learning curve:

  • The line-to-line syntax is mostly like C++.
  • There is no explicit "include" syntax needed; you can refer to public classes in the same directory by name.
  • public classes must have the name of their file, but a file may contain additional private classes.
  • it's accurate enough as you get started to think that everything that looks like an C++ object (Thing thing) is really implemented as a pointer to a reference-counted object. The "." is like C++ "->".
  • There's no passing of atomic variables by reference; always by value (int boolean double etc.)
  • String is a middle ground between value and reference
  • objects are passed by reference, thought the "reference" to them is being passed by value. Method( Thing thing ) in Java is like calling Method( Thing* pthing ) in C++. Method()'s change to thing/pthing itself doesn't affect the caller, but thing.field/pthing->field changes are global.
  • For more specific questions, Google should find everything you need.
Swiss Frank
  • 1,985
  • 15
  • 33
  • 1
    Good and detailed description, but note that JDK 8 is not the latest version - it's [JDK 11](http://jdk.java.net/11/). – Jesper Oct 22 '18 at 09:30
  • 1
    @Jesper: 8 or 11, Hello World won't change much. :D (Also, I don't have a Redhatish system around me, but maybe they meant 8 is the latest _RPM_ - it's very common for package repositories to lag behind with versions) – Amadan Oct 22 '18 at 09:33
  • *"Objects are passed by reference"* – nope! – MC Emperor Oct 22 '18 at 11:45
  • "Objects are passed by reference" – yep! If I call `MyMethod( Thing thing )`, and that modifies a member of thing like: `thing.i++`, the caller will see the modification upon return, as will the rest of the program immediately. I'll meet you half-way and would agree that "the reference to an object is passed by value," if that was what you mean, but that's not what you wrote. We can't say: `thing = null` and have that affect the caller. – Swiss Frank Oct 22 '18 at 12:11
  • 1
    @SwissFrank [There's a subtle, yet important difference between passing by reference and passing by value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – MC Emperor Oct 22 '18 at 13:49
  • There's not a subtle difference, there's a HUGE difference. In Java, clearly, the object itself is passed by reference. (That reference is passed by value but that's not the topic.) The Q/A you link to is phenomenally confused. Top answer contains this: "Java is always pass-by-value. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it." Of those three lines, first is false; second is debatable; first 1/2 of third is apparently impossible, and second 1/2 is WHAT I'M SAYING. – Swiss Frank Oct 22 '18 at 13:56
  • The answer is confusing and the question is locked. The truth is, Java is "pass by value where value is sometimes a reference". Which is a mouthful. Pass by value and pass by reference are very old terms; the former meant the whole value is cloned (not the case, unless you are aware that "value" is actually a reference: the point of confusion); the latter meant you could pass a variable into a function and the variable's assignment could change (not the case). Let's just use a new term - Wikipedia has "pass by sharing"; I also acknowledge "pass by reference value" as a shortcut for the above. – Amadan Oct 23 '18 at 02:46