3

I am having a problem using the custom jar libraries (algs4.jar/stdlib.jar from http://algs4.cs.princeton.edu/home/) with the command prompt. I added the libraries to the IntelliJ classpath setting (project sturcture -> SDKs -> classpath) and am able to use their classes with no problems using IntelliJ.

However, I also want to be able to use these libraries in the command prompt. Even though I had no CLASSPATH variable set in my Windows settings I've been able to use javac/java in cmd with no problems when using the standard java libraries. (Probably because IntelliJ sets the CLASSPATH for all the standard libraries on every startup/compile to work systemwide). However, even after adding the 2 jars to the IntelliJ classpath setting, I wasn't able to use "javac" in cmd. I then created the CLASSPATH variable in windows settings, and added the jars to them. After this I was able to compile with javac with no problems. However, when I try to run the program in cmd, I get this:

"Error: Could not find or load main class ".

I get this error whenever I uses ANY of the jar libraries, including the standard ones, with which I didn't have problems prior to setting the Windows CLASSPATH. I guess what happens is once I set my own CLASSPATH this overrides the classpath set by intelliJ. When I removed the CLASSPATH, I was once again able to compile and run the standard libraries in cmd, but not the 2 custom libraries.

Please help!

2 Answers2

6

You should make these libraries a part of the project by adding them to the module dependencies as a library instead of the JDK, then you can produce an artifact jar file that will have all the dependencies inside or near the jar and referenced via the jar manifest file classpath. See also the artifacts help section.

Community
  • 1
  • 1
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • I followed the ["Creating a module library and adding it to module dependencies" part](http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html) with IntelliJ IDEA 12 Mac. If I try to use a class defined in the jar, it warns that "Cannot resolve symbol 'XXX'". Any idea? This has nothing to do with "artifacts" I think as I'm not trying to export the project. – qazwsx Mar 27 '14 at 03:47
1

As this post points out, in this particular case the problem likely is due to the fact that that the classes in the library are "...placed in the default package (no package statement), and modern versions of java (at least 7 and 8) forbid to refer classes in default package, unless your own class is in default package too."

To solve the problem, I extracted the .java files from the jars (change the filetype from .jar to .zip and extract to a directory). I made up a package name, "com.sedge.stdlib", and modified the .java files as needed. For example, I modified the StdLib.java class from algs4 by placing a package directive at the top:

package com.sedge.stdlib;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

/**
 *  <i>Standard output</i>. This class provides methods for writing strings
 *  and numbers to standard output.
 *  <p>
 *  For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
 *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
 *
 *  @author Robert Sedgewick
 *  @author Kevin Wayne
 */
public final class StdOut {
. . .
Bex
  • 573
  • 2
  • 6
  • 16
  • How did you make a new jar file from them? Was it just a question of recompiling all the .java files? What about the META-INF and other such things that were in the jar? – David Jun 10 '15 at 00:25
  • For my purposes, I was content to have the code itself in the project. Creating a JAR would be fairly straightforward. The META-INF's MANIFEST.MF file is trivial; it's contents consist of: ` Manifest-Version: 1.0 Created-By: 1.7.0_75 (Oracle Corporation) ` Your development environment would generate a reasonably similar manifest, I imagine. – Bex Jun 11 '15 at 16:13
  • After posting that question, I discovered that I could just use JetBrains IDEA to "create project from existing source" and then "Build Artifact". I've actually converted the entire sedgewick library to work with Java 1.8 and jogl 2.0, etc. – David Jun 11 '15 at 20:01