Getting Library errors when compiling with javac.

huangapple go评论80阅读模式
英文:

Getting Library errors when compiling with javac

问题

You are getting errors because it seems that you are trying to use Java 8 features (specifically, java.util.stream) with Java 7. To resolve this issue, you should either update your Java version to a version that supports these features (Java 8 or higher) or modify your code to use features available in Java 7.

If you choose to stick with Java 7, you'll need to refactor your code to avoid using Java 8 stream features like java.util.stream.IntStream.

If you decide to upgrade to a newer version of Java, make sure your environment is correctly configured to use the updated JDK.

Regarding your question about including libraries, Java does include standard libraries automatically, but in this case, the java.util.stream package is not available in Java 7, which is why you are encountering these errors.

英文:

I get this error when i try to compile my code with javac on command prompt.
i am using jdk 1.7.0_42 with Windows 10

Exercise1.java:2: error: package java.util.stream does not exist
import java.util.stream.IntStream;
                       ^
Exercise1.java:19: error: cannot find symbol
            Arr = IntStream.range(0,size).toArray();
                  ^
  symbol:   variable IntStream
  location: class Exercise1
Exercise1.java:31: error: cannot find symbol
            Arr=random.ints(size).toArray();
                      ^
  symbol:   method ints(int)
  location: variable random of type Random
3 errors

My code is as follows

import java.util.Random;
import java.util.stream.IntStream;

public class Exercise1 {
    public static void main(String[] args) {
        String order = args[0];
        int size = Integer.parseInt(args[1]);
        if (size < 1)
        {
            System.out.println("Size must be a positive integer");
            return;
        }
        String algo = args[2];
        String outputfilename = args[3];
        int[] Arr = new int[size];
        if(order.equals("ascending"))
        {
            Arr = IntStream.range(0,size).toArray();
        }
        else if(order.equals("descending"))
        {
            for (int i= size -1 ;i>=0;i--)
            {
                Arr[i]=i;
            }
        }
        else
        {
            Random random =new Random();
            Arr=random.ints(size).toArray();
        }
        System.out.println(Arr);
    }
}

Command i write on command prompt

javac Exercise1.java

What am i doing wrong ? Do i need to compile my code with java.util ?
doesn't java include these libraries automatically ?

答案1

得分: 2

stream 已经在 Java 8 中引入,即 Java 1.8.* 及更高版本。因此,您至少需要安装 jdk-8jdk-7 或更低版本不支持流(stream)功能。

英文:

stream has been introduced in java 8 i.e. java 1.8.* and later. So, you've to install jdk-8 at least. jdk-7 or lower version does not have stream support....

huangapple
  • 本文由 发表于 2020年8月12日 12:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63370003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定