-1

Here are some questions about setting the CLASSPATH in IntelliJ:

Most of the answers involve changing settings in the Project Settings -> Modules -> Sources or -> Dependencies pages. But the word "CLASSPATH" isn't actually on those pages.

For example, the -> Dependencies page lets one explicitly set a place to find other .class files or .jar files. But no "CLASSPATH" anywhere.

Also, setting a location in the -> Sources page seems to indicate what the directory tree for the .class root should look like, but the actual location is actually set in the Project Settings -> Project -> Project Compiler Output field, again without the word "CLASSPATH".

Is JetBrains trying to hide this word for some reason (eg. it's meant to be a multi-language IDE)? Is there some place to explicitly set the CLASSPATH other than an env variable?

Community
  • 1
  • 1
jordanpg
  • 6,386
  • 4
  • 46
  • 70
  • You're asking for an opinion? – Software Engineer Sep 11 '14 at 16:01
  • @EngineerDollery, no, I'm asking why it's absent from the settings and if it's possible to set it explicitly without using various tabs and setting that use different words for it. – jordanpg Sep 11 '14 at 16:06
  • 1
    Because build, compile, test, and run, may all use different sets of dependencies for different reasons. Classpath is extremely limited and doesn't describe these different scopes. – Software Engineer Sep 11 '14 at 16:10
  • @EngineerDollery, I appreciate that it may vary by tool, build environment, etc. I'm specifically asking why IntelliJ doesn't use the word CLASSPATH when most of the canonical Java docs do. I would have also expected a place to explicitly append to a given environment's CLASSPATH in the settings on a per-module/project/env basis. I didn't mean to annoy everyone with this question; I found it counterintuitive and was just curious. – jordanpg Sep 11 '14 at 16:53
  • As I tried to point out -- classpath is a subset of dependency with a specific scope. IntelliJ doesn't talk about classpath directly because it needs to deal with the larger concept, and to deal directly with the subset would make everything a lot more confusing. – Software Engineer Sep 11 '14 at 17:57
  • @EngineerDollery At the JVM level, what other way is there to specify Java dependencies besides classpath? What is it a subset of? – jordanpg Sep 11 '14 at 18:11

2 Answers2

2

As you say, the CLASSPATH is an environment variable, so it would affect all applications referencing that variable:

The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

Source: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

You can also use the -classpath option when running your application, which would not affect other Java applications running on your system.

IntelliJ will construct your classpath to include both your application and any dependencies (whether folders or Jars etc.) and passes that to the JVM when you run your application. The IntelliJ GUI does refer to your explicit "Dependencies" and let you edit them. Your application build path would need to contain those dependencies and all of your application code, so it makes sense to include that implicitly.

If you were to use a lot of libraries, your classpath could become very long - having to maintain the list of locations in the class search path manually would be time consuming and (human) error prone - a typo on a folder name and it won't compile / run - so it assumes the task for you.

You'll probably find your IDE passes a number of other arguments to the JVM on your behalf (heap size, GC, JMX extensions and so on) - but that's part of what your IDE is for. Sure, we could invoke things like version control ourselves from the command-line, but why forgo the help? Your IDE isn't trying to "hide" anything from you, it's (hopefully) providing a more intuitive interface for many common development tasks.

Michael
  • 7,348
  • 10
  • 49
  • 86
0

CLASSPATH happens to be the name of the environment variable used automatically by Java.

It's generally considered a better option to use the -cp argument to explicitly set the classpath rather than fighting with CLASSPATH conflicts since it's process-wide.

It's not trying to "hide" it at all; there's just no reason to use it.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I understand that, but isn't the `-cp` flag for CLI use? What does this have to do with setting the `CLASSPATH` on a per-project or per-module basis in IntelliJ? – jordanpg Sep 11 '14 at 16:04
  • @jordanpg JVM args are JVM args; similar to how you can define system variables, memory options, etc. for the process that runs your code. I guess I just don't see what the issue actually is here. – Dave Newton Sep 11 '14 at 16:32
  • There is a lot of ink spilled in intro Java material about the classpath. It was counterintuitive for me to find it absent from any of the settings pages for one of the premiere Java IDEs. – jordanpg Sep 11 '14 at 16:42
  • I use Eclipse, but there's no direct mention of classpath I'm aware of. Instead, you have "Java Build Path" in the properties menu where you set your app source structure, project and library dependencies. – Michael Sep 11 '14 at 16:47
  • @jordanpg ... It's not in most IDEs as an all-upper-case environment variable, because it's not an environment variable in those circumstances. – Dave Newton Sep 11 '14 at 20:10