Wednesday, November 29, 2017

specify the Java compiler version in pom.xml

For example, if you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), you can either add the two following properties, which are the default property names for the plugin parameters:
  1. <project>
  2. [...]
  3. <properties>
  4. <maven.compiler.source>1.8</maven.compiler.source>
  5. <maven.compiler.target>1.8</maven.compiler.target>
  6. </properties>
  7. [...]
  8. </project>
or configure the plugin directly:
  1. <project>
  2. [...]
  3. <build>
  4. [...]
  5. <plugins>
  6. <plugin>
  7. <groupId>org.apache.maven.plugins</groupId>
  8. <artifactId>maven-compiler-plugin</artifactId>
  9. <version>3.7.0</version>
  10. <configuration>
  11. <source>1.8</source>
  12. <target>1.8</target>
  13. </configuration>
  14. </plugin>
  15. </plugins>
  16. [...]
  17. </build>
  18. [...]
  19. </project>
@reference_1_stackoverflow.com
How do you specify the Java compiler version in a pom.xml file?
@reference_2_stackoverflow.com
Specifying java version in maven - differences between properties and compiler plugin
@reference_3_maven.apache.org
Setting the -source and -target of the Java Compiler

Wednesday, November 22, 2017

import static in Java

example.
import java.lang.Math;

class WithoutStaticImports {

 public static void main(String [] args) {
  System.out.println("round " + Math.round(1032.897));
  System.out.println("min " + Math.min(60,102));
 }
}
Same code, with static imports:
import static java.lang.System.out;
import static java.lang.Math.*;

class WithStaticImports {
  public static void main(String [] args) {
    out.println("round " + round(1032.897));
    out.println("min " + min(60,102));
  }
}
Note: static import can make your code confusing to read.

What does the “static” modifier after “import” mean?

Saturday, November 18, 2017

Can't execute jar- file: “no main manifest attribute”

For Maven, something like the following snippet should do the trick. Note that this is only the plugin definition, not the full pom.xml:
<build>
  <plugins>
    <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.mypackage.MyClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

@reference_1_stackoverflow.com

Eclipse/Maven error: “No compiler is provided in this environment”

Go to Window → Preferences → Java → Installed JREs.
And see if there is an entry pointing to your JDK path, and if not, click on Edit button and put the path you configured your JAVA_HOME environment.

Monday, November 6, 2017

Import Projects from Git

Eclipse platform requires a .project file to import files into the workbench. EGit allows to add repositories which don't contain .project files but you have to create them in order to import them into the Eclipse workbench. This can be done using the following commands:

from (Project, Package, ...) explorer views
- Import... > Git > Projects from Git
- Import... > Git > Projects from Git (with smart import) if you installed the experimental feature "Git Team Provider - experimental auto-import (Incubation)"
from Git repositories view:
- add or clone repository
- then select the repository node and click "Import projects"


@reference_1_eclipse.org 
Possible to use Egit on legacy code without .project. .cproject


Saturday, September 23, 2017

read() method return -1 when reach the End of Stream

read()

The read() method of an InputStream returns an int which contains the byte value of the byte read. Here is an InputStream read() example:
int data = inputstream.read();
You can case the returned int to a char like this:
char aChar = (char) data;
Subclasses of InputStream may have alternative read() methods. For instance, the DataInputStream allows you to read Java primitives like int, long, float, double, boolean etc. with its corresponding methods readBoolean(), readDouble() etc.

End of Stream

If the read() method returns -1, the end of stream has been reached, meaning there is no more data to read in the InputStream. That is, -1 as int value, not -1 as byte or short value. There is a difference here!
When the end of stream has been reached, you can close the InputStream.

@reference_1_tutorials.jenkov.com
Java IO: InputStream

Reading from a Socket

To read from a Java Socket you will need to obtains its InputStream. Here is how that is done:
Socket socket = new Socket("jenkov.com", 80);
InputStream in = socket.getInputStream();

int data = in.read();
//... read more data...

in.close();
socket.close();
Pretty simple, right?
Keep in mind that you cannot always just read from the Socket's InputStream until it returns -1, as you can when reading a file. The reason is that -1 is only returned when the server closes the connection. But a server may not always close the connection. Perhaps you want to send multiple requests over the same connection. In that case it would be pretty stupid to close the connection.
Instead you must know how many bytes to read from the Socket's InputStream. This can be done by either the server telling how many bytes it is sending, or by looking for a special end-of-data character.

@reference_2_tutorials.jenkov.com
Java Networking: Socket