Hot questions for Using Ubuntu in java 8

Question:

After installing Intellij Idea 14, with OracleJDK 1.8 u25 (64 bit) on my 14.04 Ubuntu system, I cannot configure the Java SDK to use any JDK. When I navigate to Module Settings->SDK and click configure SDK, the add button, and then select /usr/java/jdk1.8.0_25 in the dialog, I get an error popup stating that "The selected directory is not a valid home for JDK."

I've checked the contents of the directory, which contains a bin folder with both java and javac inside, a lib folder and a jre folder.

I've tried selecting each of those in turn, to no avail, which I expected.


Answer:

I had the same problem, in my case I wasn't able to select any of the "openjdk" directories under /usr/lib/jvm/, I solved with:

sudo apt-get install openjdk-8-jdk

When restarting intellij it will find the path to the jdk automatically.

Question:

I have little maven project that uses com.sun.istack.internal.Nullable. I tried both JDK 9 and 1.8, and still get error about package does not exists. I search for the package and find some: https://mvnrepository.com/search?q=+com.sun.istack but they still not supply necessary one.

The JAVA_HOME:

apshenichnikov@IAS-WS-UX02:~/NetBeansProjects/newlps$ echo $JAVA_HOME
/usr/local/java/jdk1.8.0_121/

And JDK:

apshenichnikov@IAS-WS-UX02:~/NetBeansProjects/newlps$ sudo update-alternatives --config java
There are 4 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                     Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-9-oracle/bin/java       1091      auto mode
  1            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      manual mode
  2            /usr/lib/jvm/java-9-oracle/bin/java       1091      manual mode
* 3            /usr/local/java/jdk1.8.0_121/             1         manual mode
  4            /usr/local/java/jdk1.8.0_121/bin/java     1         manual mode

Press <enter> to keep the current choice[*], or type selection number: 

And the POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ru.beinteractive</groupId>
    <artifactId>newlps</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>newlps</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.google.code.externalsortinginjava</groupId>
            <artifactId>externalsortinginjava</artifactId>
            <version>0.1.9</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <compilerArgument>-XDignore.symbol.file</compilerArgument>
                </configuration>
            </plugin>           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/libs
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>           
        </plugins>      
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

I injected:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <compilerArgument>-XDignore.symbol.file</compilerArgument>
    </configuration>
</plugin>

Inside the POM.xml

And I still have compilation exception

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.286 s
[INFO] Finished at: 2017-12-13T15:28:45+03:00
[INFO] Final Memory: 20M/261M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project newlps: Compilation failure: Compilation failure:
[ERROR] /home/apshenichnikov/NetBeansProjects/newlps/src/main/java/su/ias/utils/ParamUtils.java:[3,31] package com.sun.istack.internal does not exist
[ERROR] /home/apshenichnikov/NetBeansProjects/newlps/src/main/java/su/ias/utils/StringUtils.java:[3,31] package com.sun.istack.internal does not exist
[ERROR] /home/apshenichnikov/NetBeansProjects/newlps/src/main/java/su/ias/utils/ParamUtils.java:[36,74] cannot find symbol
[ERROR] symbol:   class Nullable
[ERROR] location: class su.ias.utils.ParamUtils
[ERROR] /home/apshenichnikov/NetBeansProjects/newlps/src/main/java/su/ias/utils/ParamUtils.java:[46,68] cannot find symbol
[ERROR] symbol:   class Nullable
[ERROR] location: class su.ias.utils.ParamUtils
[ERROR] /home/apshenichnikov/NetBeansProjects/newlps/src/main/java/su/ias/utils/StringUtils.java:[19,36] cannot find symbol
[ERROR] symbol:   class Nullable
[ERROR] location: class su.ias.utils.StringUtils
[ERROR] /home/apshenichnikov/NetBeansProjects/newlps/src/main/java/su/ias/utils/StringUtils.java:[45,6] cannot find symbol
[ERROR] symbol:   class Nullable
[ERROR] location: class su.ias.utils.StringUtils
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Answer:

Try import import org.jetbrains.annotations.Nullable instead. It has the same you need for @Nullable annotation.

Then add this dependency:

<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>13.0</version>
</dependency>

Question:

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

I use Eclipse, Maven and java8-openjdk.

  1. I am using JDK not JRE: Java is located at **/usr/lib/jvm/java-8-openjdk-amd64/**.

  2. The class with main method compiles and runs without a problem.

  3. I was able to build before without any problems.
  4. The runtime version of Eclipse:

    java.runtime.version=1.8.0_91-8u91-b14-0ubuntu4~16.04.1-b14
    
  5. In pom.xml, I have Maven compiler plugin, with version 3.5.1, source and target values 1.8.

What am I missing here ? I have configured correct path. I have JDK. I did builds before and yet Maven is failing now.

Please let me know how to start about finding the root cause and also let me know if any more information is needed. Thanks.


Answer:

Seems like maven-compiler-plugin version 3.x is using tools.jar for compilation rather then javac, please make sure this jar is present under $JAVA_HOME/lib and eclipse has permissions to access it.

You should also verify that tools.jar exists under Windows->Preferences->Installed JREs, and if it is not, add it manually.

Question:

Given:

A clean Ubuntu 16 with these versions of java and a clean Payara installed:

jdk-8u241-linux-x64.tar.gz
payara-5.194.zip

When:

./asadmin start-domain

Then: Yields this output:

Exception in thread "main" java.io.IOError: java.lang.UnsupportedOperationException
    at org.jline.utils.Curses.tputs(Curses.java:62)
    at org.jline.utils.Curses.tputs(Curses.java:45)
    at org.jline.keymap.KeyMap.key(KeyMap.java:243)
    at org.jline.reader.impl.LineReaderImpl.key(LineReaderImpl.java:5784)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at org.jline.reader.impl.LineReaderImpl.bindKeys(LineReaderImpl.java:5792)
    at org.jline.reader.impl.LineReaderImpl.emacs(LineReaderImpl.java:5503)
    at org.jline.reader.impl.LineReaderImpl.defaultKeyMaps(LineReaderImpl.java:5479)
    at org.jline.reader.impl.LineReaderImpl.<init>(LineReaderImpl.java:277)
    at org.jline.reader.LineReaderBuilder.build(LineReaderBuilder.java:115)
    at com.sun.enterprise.admin.cli.CLICommand.buildLineReader(CLICommand.java:1445)
    at com.sun.enterprise.admin.cli.CLICommand.prevalidate(CLICommand.java:941)
    at com.sun.enterprise.admin.cli.CLICommand.execute(CLICommand.java:325)
    at com.sun.enterprise.admin.cli.AdminMain.executeCommand(AdminMain.java:360)
    at com.sun.enterprise.admin.cli.AdminMain.doMain(AdminMain.java:294)
    at org.glassfish.admin.cli.AsadminMain.main(AsadminMain.java:56)
Caused by: java.lang.UnsupportedOperationException
    at org.jline.utils.Curses.doTputs(Curses.java:78)
    at org.jline.utils.Curses.tputs(Curses.java:60)
    ... 23 more

For the next few months we wont be able to upgrade Ubuntu yet:(

I think the latest version of jline is causing issues; does anyone have an idea how work around this?


Answer:

The behaviour you are seeing is due to a bug in the JLine library.

See here for reference: need support securtCRT terminal types - Linux and Fails with an UnsupportedOperationException by jline 3.10 and 3.11.0

According to another issue reported against Apache Karaf (KARAF-6341) it seems to be caused by the setting TERM=linux.

So as a workaround you could issue the command

export TERM=xterm

in a shell and start Payara from that shell.

Question:

I'm trying to run a curl command that consumes a webservice I'm running:

String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(curl);
    process.waitFor();
} catch (Exception e) {
    e.printStackTrace();
}

If I copy the log message from the Sysout and paste it on my terminal, it runs as expected, but when I run the java code, it seems to return an html page from the proxy not finding the service.

Do I have to add something else for it to run from java?


Answer:

The problem is that you don't have it read the output. I modified your code so it should work. I'm not 100% sure because I can't test it properly for some reason.

EDIT - It works, I was just executing the wrong command! This should work for you.

String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(curl);
    process.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //BufferedReader to read the output
    StringBuilder sb = new StringBuilder(); //What will hold the entire console output
    String line = ""; //What will hold the text for a line of the output
    while ((line = reader.readLine()) != null) { //While there is still text to be read, read it
        sb.append(line + "\n"); //Append the line to the StringBuilder
    }
    System.out.println(sb); //Print out the full output
} catch (Exception e) {
    e.printStackTrace();
}

EDIT - New code utilising ProcessBuilder instead of Runtime

String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", curl);
builder.redirectErrorStream(true);
Process p = builder.start();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
int linenum = 0;
while (true) {
    linenum++;
    line = r.readLine();
    if (line == null) {
        break;
    }
    sb.append(line);
}
System.out.println(sb);

Question:

I have an Intellij project. I compile ('Make') it on Windows, and sends it onto a Ubuntu 14.04 remote server, running Java 1.8.

My problem is that the System.out.println() outside of the main method prints nothing. I've spent a few hours on this problem, and I can ensure that :

  • I know the methods are executed because they raise Warnings : Aug 20, 2016 7:35:36 PM org.apache.pdfbox.cos.COSDocument finalize WARNING: Warning: You did not close a PDF Document

  • When the execution ends, I always get the following output for rates : []

  • Printing the methods as I call them & place print lines at the begining of these methods, won't do the stuff.

N.B. To execute my project, I use : java MyProject.TestAlgo , with TestAlgo the main Class.


EDIT : I expect the output to be a List such as : [1.0, 2.3, ..., 5.4], but I get []

I don't think the problem is programming problem, because whenever I compile it on my computer which runs Windows, I get the prints from outside the main.

When I compile the project, and run it on the Ubuntu remote server, I don't see any output except the prints from the main, and the warnings raised by methods from outside main class. All methods trying to print and called in the main are declared static.


Answer:

Solved :

I was compiling Java with the 1.8.0.101 JDK version on Windows, and running it with the 1.8.0.91 on Ubuntu, which (I think) must have created version conflicts..

I solved the issue by updating Java JRE so I had same versions of Java onto Windows and Ubuntu.