-2

I have downloaded a library which contains some methods within some classes that I would like to use.

The library I have downloaded is the apache common lang package and I have the "commons-lang3.jar" file, I am trying to set a classpath using the environment variables in windows 7 but it doesn't do anything.

Can someone check if I am doing this right:


User variables for Me:

Variable: PATH,

Value: C:\Users\Me\Documents\JavaLib\commons-lang3.jar,

JavaLib is a file I have created which contains commons-lang3.jar on its own.


Java file name: Myclass.java

command typed into cmd: javac Myclass.java

Error returned: error: package org.apache.commons.lang3 does not exist

Myclass.java contains:

import org.apache.commons.lang3.StringUtils;

and it can't find it even though I have put it into my environment variables.

What have I done wrong?

asgs
  • 3,928
  • 6
  • 39
  • 54
Michael Howlard
  • 217
  • 2
  • 8
  • See, for example: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html and http://docs.oracle.com/javase/tutorial/essential/environment/paths.html – DNA Jun 02 '14 at 21:47

1 Answers1

2

The environment variable to use is classpath. Also, setting the classpath beyond your application's lifecycle isn't recommended especially when there are multiple apps running with different versions of the same library. Just specify the library location in the -classpath option to the java and javac commands. Or atleast set it in a batch/shell script file that will set the variable, open the application and unset implicitly upon termination.

asgs
  • 3,928
  • 6
  • 39
  • 54
  • Thanks for the answer. I have another problem: when I type "java Myclass" it returns the error: "Error: Could not find or load main class Myclass". What is wrong here? – Michael Howlard Jun 02 '14 at 21:49
  • do you have this class inside some package? if so, read this post on [how-can-i-compile-classes-in-packages-to-execute-them-later-with-java-program](http://stackoverflow.com/questions/4910964/how-can-i-compile-classes-in-packages-to-execute-them-later-with-java-program) – asgs Jun 02 '14 at 21:51
  • No this class is just on it's own. It is just a simple Application. – Michael Howlard Jun 02 '14 at 21:52
  • OK, go ahead and paste your source code. Can't guess much as to what's going on. – asgs Jun 02 '14 at 21:53
  • There shouldn't be a problem with the source code since it compiles correctly, is that correct? – Michael Howlard Jun 02 '14 at 21:59
  • The reason I ask is, people sometimes mess up the `main` method's signature. Missing a few keywords like static or returning non-`void` could cause Java to throw this error. Anyway, it's worth taking a look at this answer [error-could-not-find-or-load-main-class#answer-7485781](http://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class#answer-7485781) – asgs Jun 02 '14 at 22:02
  • I can't fit the source code into a comment and I am only allowed to post once every 90 minutes, I have read that post previously but I can't see a fix. – Michael Howlard Jun 02 '14 at 22:12