2

I'm new to java that goes without saying. I'm using IntelliJ. I've stumbled upon a problem I just don't understand. I've made a simplified program that illustrates my problem. It just checks if number is in an array.

import org.apache.commons.lang.ArrayUtils;


public class Test {
    public static void main(String[] args) {
        int[] list = new int[]{1,2,3};
        Boolean help = ArrayUtils.contains(list, 3);
        System.out.println(help);
    }
}

(ctrl+shift+f10) runs good and returns true, however when i go and try to compile:
E:\...\W1>javac test.java
I get:

test.java:1: error: package org.apache.commons.lang does not exist
        import org.apache.commons.lang.ArrayUtils;
        ^
        test.java:7: error: cannot find symbol
        Boolean help = ArrayUtils.contains(list, 3);
        ^
        symbol:   variable ArrayUtils
        location: class test
2 errors

I have downloaded the commons-lang-2.6 and marked the folder as a library to the module that my program is in. I've even added the folder to the CLASSPATH. Any suggestions how to fix it and explanations why had this happened will be appreciated.

Michał Gigoń
  • 90
  • 1
  • 1
  • 8
  • I don't see any classpath... – shmosel Aug 10 '17 at 09:00
  • 2
    If you are compiling with javac, you must add `-cp` option for specifiying the classpath – Dimitri Aug 10 '17 at 09:01
  • @michal gigon please read this [add external jar in intellij](https://stackoverflow.com/a/1051705/8035260) – jose praveen Aug 10 '17 at 09:05
  • 2
    You should be using commons-lang3, not the older version. You should not be manipulating any CLASSPATH environment variable - IntelliJ ignores it. You have to put your dependency JARs in a folder /lib in your project and mark it as a JAR source. Do yourself a favor and Google Java coding standards. Your simple code doesn't follow them. You cannot succeed at runtime but fail to compile. You are confused. – duffymo Aug 10 '17 at 09:05
  • Just copy-paste the .jar under the libs folder, right click on it and select 'Add as library' option from the list. It will do the rest... – Rakesh Soni Aug 10 '17 at 09:07
  • @duffymo: You certainly don’t normally put your dependencies into a lib-folder into your project. You should set up a Maven or Gradle build instead that handles the dependencies. – Michael Piefel Aug 10 '17 at 09:07
  • 1
    I certainly use Maven. We have a "new to Java" who can't tell the difference between compile and runtime and you want to suggest Maven? Why not give him a loaded handgun, too? I'd err on the side of "keep it simple". – duffymo Aug 10 '17 at 09:08
  • 1
    Absolutely. Do you think it's beyond him to learn that he should capitalize his class names? You disagree with that, but you recommend Maven? – duffymo Aug 10 '17 at 09:12
  • @Dimitri Thanks your advice worked. duffymo citing Kirk Lazarus 'I am a little confused' :) – Michał Gigoń Aug 10 '17 at 09:13

3 Answers3

0

Your IntelliJ uses a different class path to compile (and run) your program than your command line invocation. Whatever you configured as dependencies has to be added to the javac call as well.

Much better still: Create a Maven or Gradle project (IntelliJ has ample support) and use that to compile the project. IntelliJ will use the Maven configuration, and on the command line you invoke mvn package to compile and test and package everything.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
0

IntelliJ compiles your app in a more sophisticated way - it adds that library to the classpath.

Why would your program compile? How your attempt differs from storing that array in a random directory on your machine?

If you really want to compile using command line just to see it works, add this jar to the folder your test class is. Rename it to Test please. You can point it using -cp flag.

For the future, you should learn Maven - it solves many problems with building applications.

xenteros
  • 15,586
  • 12
  • 56
  • 91
0

Get apache commons jar from http://www.java2s.com/Code/Jar/a/Downloadapachecommonsjar.htm

Follow steps mention in here: https://stackoverflow.com/a/9395928/2987755

javac will create .class file, run it with $java

Other example: http://www.programcreek.com/2014/01/compile-and-run-java-in-command-line-with-external-jars/

dkb
  • 4,389
  • 4
  • 36
  • 54