Having trouble setting the Java classpath from terminal - java

My directories are structured as follows: /User/JAVA/MyProject/
In the MyProject folder, I have my Main.java and Helper.java files
In the JAVA folder, I have two JAR files: jar1.jar and jar2.jar
I would like to set my classpath to these two JARs so I can use them in my Main.java and Helper.java files. Very simple.
To do this, I have tried the following (as well as numerous other variations) from my terminal:
user:MyProject$ javac -cp "/User/JAVA/jar1.jar:/User/JAVA/jar2.jar:." Main.java
EDIT: I am now using this command, as suggested, and receiving the same errors
user:MyProject$ javac -cp "/User/JAVA/jar1.jar:/User/JAVA/jar2.jar:." Helper.java Main.java
From what I've read, this should work. I have specified the absolute locations of the two JARs, I have separated them by a colon : (I am on OSX), and I have also included my current directory .. Then I have specified the file containing my main method with Main.java.
Unfortunately, I keep receiving a "Cannot find symbol" for the classes' methods in the JARs when I try to compile.
Furthermore, when I try System.out.println(System.getProperty("java.class.path"));, I receive the following output ., indicating my classpath is still set to my current directory.
My error is this:
/User/JAVA/MyProject/Helper.java:13 cannot find symbol
symbol: variable StdOut
location: class Helper
To note:
When I opened my project up in Eclipse and added the classpath via the IDE, everything worked as expected and no error messages appeared during compilation. Nevertheless, I still would like to figure out how to do this from terminal and without the IDE.
What am I doing wrong, and how do I fix this?
Thanks.

Using -cp when compiling or running sets the classpath. That overrides (ignores) anything you would have put in the env variable CLASSPATH.
Since you didn't post the actual error you're receiving I'm going to guess the missing symbol is from your other class that Main is referencing. Add it to the command:
javac -cp "/User/JAVA/jar1.jar:/User/JAVA/jar2.jar:." Helper.java Main.java
Edit due to the OP posting the actual error: You have a an error in your class that makes it uncompilable. It has nothing to do with jar files or classpaths. You are attempting to use / reference a variable named StdOut that doesn't exist.

You should use the semicolon paths separator (;), as discribed in Setting the classpath document.
But you are using colon instead (:).
Try to use the following command (copy and paste it):
javac -cp /User/JAVA/jar1.jar;/User/JAVA/jar2.jar Helper.java Main.java
from your /User/JAVA/MyProject directory.
EDIT:
All above is for Windows.
The problem is in paths themselves.

Related

How to set the classpath correctly in java

I need to compile and run simple code using the gson library, but I can't use Maven, Gradle or the IDE.
The directory contains Main.java and gson-2.9.0.jar
javac -cp gson-2.9.0.jar Main.java works correctly and creates Main.class
But when I run java -cp ./*: Main, I get
Error: Could not find or load main class Main.
Caused by: java.lang.ClassNotFoundException: Main
I also tried the following commands:
java -cp gson-2.9.0.jar Main
java -cp gson-2.9.0.jar: Main
java -cp ./gson-2.9.0.jar:./* Main
But all these commands give the same result. I've never had to run code from the command line without Maven or IDEA before, so I think it's the classpath specification that's the problem. What am i doing wrong here?
If your main class is in a package (has a package ... declaration), you need to include the package name in the java call, e.g. java -cp ... mypackage.Main.
Additionally, the java documentation says about -cp / --class-path:
As a special convenience, a class path element that contains a base name of an asterisk (*) is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.
Therefore, using * for .class files does not work; instead you have to specify the directory name. Based on your question it looks like you are using Linux, and that the .class file and the JAR are in the same directory, so the following should work in your case:
java -cp gson-2.9.0.jar:. Main
(note the . after the :, indicating to include the current directory for the classpath)

How to import JAVA packages in cmd?

This is the first time I am compiling a program, and it doesn't seem to be working out. Looks like some packages are not being located - so for this question, I'll just focus on one:
Steps I've take so far:
1) setting up the System Variable Path to include java
2) in CMD.exe: jar tf log4j.jar I did this to make sure it includes log4j.Logger and it does.
3) I Shift+rightclick and open command prompt from this folder:
4) Then I enter javac TNT.java and i get the following error (along with others):
Any thoughts?
I set the classpath to the same folders with set classpath = "name of folder" no change...
edit
5) have also tried
javac -cp jdkbindirectory;jrebindirectory;theabovefolder TNT.java
I get this:
blahblahblah
You shouldn't set the classpath using an environment variable as it is bad practice. What if you accidentally change it later for a different project and your current project breaks?
When including classes in the classpath, you can include the path of the root of the package of the class, as in the folder that contains the folders in the package structure. However, when you're including a jar in your classpath, you need to put the entire path of the jar file (relative to the current working directory) all the way up to the jarname.jar.
Also, remember that by default, java looks in the current working directory and uses that as its default classpath. However, as soon as you specify a classpath it no longer does that automatically for you. Be sure that you're including your current directory in your classpath as well.
Finally, be sure to surround the classpath in quotes otherwise java might think its a part of another argument.
I would try this:
javac -cp "./;log4j.jar" TNT.java
And then to execute the class file:
java -cp "./;log4j.jar" TNT
Hope this works, good luck!

What does "Exception in thread \"main\" java.lang.NoClassDefFoundError" mean when executing java .class file?

Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.

How can I compile files when my current working directory is not the default package?

I have two files in Linux, both in the default package, AddSingleInstance.java and Finder.java
I am creating an instance of AddSingleInstance in Finder:
AddSingleInstance ai = new AddSingleInstance();
When I compile Finder.java file it gives the below error:
Finder.java:20: error: cannot find symbol
AddSingleInstance ai = new AddSingleInstance();
^
I'm compiling from a different directory. How can I get both files to compile successfully?
To fix your problem, your java files should be on your classpath so that javac knows where to find them. You can either manually set the classpath:
javac -classpath javadir javadir\Finder.java
java -cp javadir Finder
Or use the default classpath of .:$PATH. The easiest way to do that is
cd javadir
javac Finder.java
java Finder
I guess most people don't encounter this issue (I never have before!) because they run javac from their default package (the root of their source tree).
If they are in the same package then AddSingleInstance should be accessible in Finder and vice versa.
However if they are in separate packages, then add the following line at the top of Finder.java (after package declaration)
import packageName.AddSingleInstance;
Possible your source file is using Windows style line feeds.
Try using the program dos2unix to fix the line feeds and try recompiling. I'd also have to see the complete source file to help further, I'm assuming you have the imports at the top of the file.

Could not find or load main class , environment variables

I know this has come up a number of times, but previous responses just don't seem to help.
My environment variables are :
CLASSPATH C:\Program Files\Java\jre7\lib;C:\Program
Files\Java\jdk1.7.0_15\bin;
PATH C:\Program Files\Java\jdk1.7.0_15\bin;
When moving to the directory as follows C:\Users\Oli\My Documents\java I can compile using javac, but cannot runt he program using java. I know its most likely got something to do with environment variables but I cannot get it to work. P.S the error is "could not find or load main class"
Any help would be appreciated.
CLASSPATH is the place where JRE looks for classes. You've set your CLASSPATH to a value and expect to run the class from current Directory, which won't work.. for instant solution you may use
java -cp C:\Users\Oli\My Documents\java ClassName
Or undo setting CLASSPATH. Default CLASSPATH is current directory
Lets assume that your ".java" file default package ( no package defined) survivies in "C:\Src"
You dont need to set the CLASSPATH in this case.
cd C:\Src
javac MyJava.java
java MyJava
If with package say com.test
cd C:\Src
javac com\test\MyJava.java
java com.test.MyJava
However if you are not in the same folder as Source files and want to run from anywhere
set CLASSPATH=%CLASSPATH%;C:\src
javac MyJava.java or javac com\test\MyJava.java
and
java com.test.MyJava or java com.test.MyJava
Unset CLASSPATH and just use the default one provided by the JVM. Here is a link to the Java Tutorial that covers the environment variables.
Seems like the problem is not in the path...
Does your code use the 'package' statement? (i.e. package my_package;)
If so, go to 'java' directory and execute:
java my_package.MyClass
where 'my_package' is the name of... the package, and MyClass is your compiled .java file (without the .class extension).
Good luck.

Categories

Resources