Hot questions for Using Ubuntu in compilation

Top Java Programmings / Ubuntu / compilation

Question:

I had created a website in eclipse using tomcat as a server on windows 7 and it was working very fine. But now i have migrated from windows to ubuntu. I have installed java and it is working very well. But my eclipse is displaying error on each and every statement (this cannot be resolved). Please suggest how to solve this issue.


Answer:

Most likely your eclipse is referring to JRE associated with windows path. Simply go to

Project Setting -> Java Build Path -> Edit/re-add JRE

wth correct path for your ubuntu system

Question:

I have some Java code that compiles on my Mac, but fails to compile on my Ubuntu machine. Specifically, when using the static of method of List, I get the error:

error: cannot find symbol
        List<String> list2 = List.of("cat", "dog");
                                 ^
  symbol:   method of(String,String)
  location: interface List
1 error

I thought perhaps this was an issue with the Java version. Sure enough, it was using only Java 8. However, I subsequently upgraded my Ubuntu machine to use Java 11 using OpenJDK, but I still get the same issue.

The output I get from running java -version on the Ubuntu machine is:

openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment (build 11.0.1+13-Ubuntu-3ubuntu116.04ppa1)
OpenJDK 64-Bit Server VM (build 11.0.1+13-Ubuntu-3ubuntu116.04ppa1, mixed mode, sharing)

The output I get from running java -version on the Mac machine is:

java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

For compilation, I am using Gradle version 5.2.1. I am using the Gradle wrapper, so both machines are using the same version of Gradle.

Here is the file that does not compile properly.

package mypackage;

import java.util.List;
import java.util.ArrayList;

public class App {

    public static void main(String[] args) {
        // No errors on both MacOS and Ubuntu
        List<String> list1 = new ArrayList<>();
        list1.add("cat");
        System.out.println(list1);

        // Fails to compile on Ubuntu
        List<String> list2 = List.of("cat", "dog");
        System.out.println(list2);
    }
}

Answer:

List.of() was introduced in Java 9. This indicates that your update from Java 8 to Java 11 wasn't successful.

Run ./gradlew -v to see which version of Java is resolved by Gradle:

------------------------------------------------------------
Gradle 5.3.1
------------------------------------------------------------

Build time:   2019-03-28 09:09:23 UTC
Revision:     f2fae6ba563cfb772c8bc35d31e43c59a5b620c3

Kotlin:       1.3.21
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.2 (Oracle Corporation 11.0.2+9)
OS:           Linux 4.15.0-47-generic amd64

Make sure you aren't using Gradle daemon started on Java 8 before updating to Java 11 and in build.gradle force Java 11 compatibility:

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

Question:

I have exported my Eclipse Java project in Ubuntu, with all the .java and .class files in place.

The project is composed as follows:

- src
   - package1
        - file1.java
        ...
        - fileN.java
   ...
   - packageM
        - fileM.java
        ...
        - fileN.java

To run the program, I use the following command:

java -Djava.library.path="/path/to/opencv/lib" -cp lib/*:src package.to.main.class.MainClass 

Now, I have changed just a line in a class (which is not the MainClass), and I would like to recompile and run everything. However, when trying with javac in the following way:

javac path/to/main/class/MainClass.java

I obtain millions of errors since:

  • libraries are not found
  • other Java files are not linked

Some examples:

src/it/polimi/tweetcrawlingpipeline/pipeline/TweetCrawlingPipeline.java:7:    error: package org.opencv.core does not exist
import org.opencv.core.Core;
                  ^
  symbol:   class SVMSample
  location: class TweetCrawlingPipeline
  src/it/polimi/tweetcrawlingpipeline/pipeline/TweetCrawlingPipeline.java:158: error: cannot find symbol
   public GenericClassifier<SVMSample> getTextClassifier() {
               ^

How can I fix these problems?

Thanks.


Answer:

This might not be the answer you want, but it rapidly gets to be painful to compile at the command line with just javac. Unless you put your command and classpath into a shell script, it can get fiddly.

If at all possible, I would recommend using something like ant, gradle, or even maven. Well maven can be overkill, but ant is a reasonable start.

Question:

I'm trying to compile some software in Ubuntu to run it. I downloaded it and I saw there was a compile.bat file. I tried to run it with Wine but I get some errors. After that, I create a compile.sh file with same commands as compile.bat file. These commands are:

javac -extdirs lib/ core/*.java
javac -extdirs lib/ movement/*.java
javac -extdirs lib/ report/*.java
javac -extdirs lib/ routing/*.java
javac -extdirs lib/ gui/*.java
javac -extdirs lib/ input/*.java
javac -extdirs lib/ applications/*.java
javac -extdirs lib/ interfaces/*.java

I tried to run the .sh script but I get this error:

root@vbox:/media/sf_shared/one_1.4.1# ./compile.sh 
error: option -extdirs not allowed with target 11
error: option -extdirs not allowed with target 11
error: option -extdirs not allowed with target 11
error: option -extdirs not allowed with target 11
error: option -extdirs not allowed with target 11
error: option -extdirs not allowed with target 11
error: option -extdirs not allowed with target 11
error: option -extdirs not allowed with target 11

These are my java versions:

javac 11.0.4
/usr/bin/javac
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing)
/usr/bin/java

It seems like "extdirs" option doesn't exist. However, it is listed as a javac option:

-extdirs <dirs>              Override location of installed extensions

root@vbox:/media/sf_shared/one_1.4.1# javac -extdirs
error: -extdirs requires an argument
Usage: javac <options> <source files>
use --help for a list of possible options

How can I solve it? Any help will be usefull since I don't know too much about Java.


Answer:

Changing to java1.8 solve the problem as @seconf said.

Question:

So my problem is how to run java class or anything from my terminal. I have HelloWorld.class and Helloworld.java files. How do I run them over terminal on Ubuntu?


Answer:

You don't need the java file if you already have the class. Just run

java HelloWorld

Or is it Helloworld? Case is important!

Without the class, you can create it from the java file:

javac HelloWorld.java