From charlesreid1

Line 45: Line 45:


=Easily Reading from Input Files=
=Easily Reading from Input Files=
Like many components of Java, it's easy to get tripped up with the simple stuff, since there are many ways to do it. Here's the canonical way:
<pre>
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
</pre>
This allows you to pass input file contents on the command line like so:
<pre>
$ java MyProgram < data.txt
</pre>
See [[Java/Input Files]]


=Using Jar Files=
=Using Jar Files=

Revision as of 18:19, 31 May 2017

Cool: http://www.ibm.com/developerworks/library/j-robots/index.html

Getting Started

Download and install Java, if you need to.

Hello World

This will walk through compiling and running a "Hello world" example in Java.

The program

Here's a simple Java "Hello world" program:

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

Compiling

To compile this program, use the javac command:

$ javac HelloWorld.java

This will create a HelloWorld.class file, which is the Java executable.

Running

To run a Java executable, use the java command, and pass it the executable's filename without the extension:

$ java HelloWorld
Hello, world!

Measuring Performance of Java Code

See Java/Profiling and Java/Timing

Easily Reading from Input Files

Like many components of Java, it's easy to get tripped up with the simple stuff, since there are many ways to do it. Here's the canonical way:

Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

This allows you to pass input file contents on the command line like so:

$ java MyProgram < data.txt

See Java/Input Files

Using Jar Files

If you have a jar file that contains class definitions and you wish to use it, you can do the following:

  • To check contents of the jar, use the jar utility - it works a lot like tar
  • jar xf - expands (shows contents of) jar

Compile

To compile code mysource.java with a jar file called org.example.jar:

On Linux/Mac:

javac -cp '.:org.example.jar' mysource.java

On Windows:

javac -cp .;org.example.jar mysource.java

After this, you obtain the bytecode file mysource.class

Run

Now run the bytecode file:

java -cp '.:org.example.jar' mysource
java -cp .;org.example.jar mysource


Libraries

awesome-java repo on github: https://github.com/akullpp/awesome-java

Immutables in Java (similar notation to javascript/d3): https://immutables.github.io/

libgdx for 3d games in Java: https://libgdx.badlogicgames.com/

stanford core NLP in java: https://stanfordnlp.github.io/CoreNLP/

legion of the bouncy castle (lightweight crypto in Java): https://www.bouncycastle.org/java.html

Selenium web browser test suite: http://docs.seleniumhq.org/

jsoup crawler and html parser https://jsoup.org/

twitter4j twitter client: http://twitter4j.org/en/index.html

crawler4j another crawler and html parser https://github.com/yasserg/crawler4j

Apache Shiro for session management (large enterprise or small mobile) https://shiro.apache.org/