I am trying to compile and run java code that relies on a jar package. I put everything in the same directory and I am still getting this error.
I have no idea why this is. I imported all the jar files to my classpath too.
I guess your code use classes stored in a external jar file.
If you are using command line to compile and execute, you must add the jar file to classpath.
Ex.:
To compile:
javac -classpath C:/folder/MyJarFile.jar MyClass.java
To run:
java -classpath C:/folder/MyJarFile.jar MyClass
Also, make sure you are correctly importing the external classes, as
import thepkg.OtherClass;
I strongly advice to use an IDE, as Eclipse or netbeans do develop anything, even a basic simple example.
Today there are some online IDEs, like Tutorials Point, but I don't know how to include external libs in theses environments.
Related
I'm using the Netbeans IDE but im trying to compile and run my
project via the unix shell:
I have a package: project/src/packageName/.java files
And my jars: project/lib/.jar files
The classes under src/packagename/.. more or less depend on each other and they're also using said libraries.
I added the libraries via Netbeans (though
NetBeans couldnt recognize the classes from the libraries I used in
my project, so I needed to extract the .jar files first and direct
NetBeans to the extracted jar folders containing the .class files
from the library).
When I run the class containing the main() method in NetBeans it works just fine.
But I'm trying to accomplish this with the unix shell using javac.
My question: How do I compile my project including the jars/foreign library.
I already tried the following:
javac -classpath ".:jar1.jar:jar2.jar:" /path/to/project/src/packageName *.java
It still said that symbol cannot be found (refering to classes from library)
Sorry if this question was answered somewhere else already, but after a few hours of research I couldn't get it working.
I guess I'm doing something horribly wrong?
Do read Java documentation for javac, here is JDK14 javac.
There are plenty of other options you can try especially if you want to define different file encodings or JRE source/targets.
You can simplify command if using relative path from current directory, just cd to your project directory first. Here is a simple call:
javac -d build/my.classes -sourcepath src -cp lib/abc.jar:lib/xyz.jar src/packageName/*.java
You may need to mkdir build or some other directory to store temporary files. After this you could try running your app with direct command for java on the files.
It might be a silly question but I didn't figured it out how to use it.
I have downloaded JFreeChart from
http://sourceforge.net/projects/jfreechart/files/latest/download?source=files
and I don't use Eclipse or Netbeans or Intellij or any other. How can I compile my project within these files on command line ?
Thanks is advance..
Extract zip file you have just downloaded. Copy jars from lib folder to your lib folder and add all the jars to your classpath using -cp switch.
However what are you going to do then? If you do not use IDE you can write code using any editor you want however it is at least 10 times slower than using IDE. Managing dependencies manually and compiling code using command line compiler is possible too but it starts to be extremely complicated and time consuming once you have external dependencies (as in your case).
So, if you want to create something beyond hello world take you time and start working with build tool like maven or gradle and IDE.
Suppose that I have my project structure as following:
hello
src
Hello.java
classes
lib
one.jar
two.jar
In this case I have compile it using command
javac -cp ../lib/one.jar:../lib/two.jar Hello.java
run this command from src folder.
Use ; instead of : if you are on windows.
I am trying to compile an application from the command line
The programme compiles and runs fine in eclipse, howvever when i goto the command line and use javac to compile my java file i get 23 errors, the majority of which are Cannot Find Symbol, with an arrow pointing to the . in a package name.
Does anyone have any ideas on what i need to do differently?
Thanks
Your classpath is not set up correctly. Look at your Eclipse project in the .classpath file. In there you will find a lot of classpathentry elements. You will need to replicate this for your command line compilation.
To do this manually you must first set your CLASSPATH environment variable to a list of directories (or jar files) containing class definitions.
You can also use a build tool called ant to automate this for you.
I advise against setting the classpath as an environment variable because it is too intrusive (all your java programs will see it).
A command line for compiling a Java app which dependes on Log4j might look like this:
javac -cp C:\dev\mvn\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar AppenderTester.java
If you have multiple classpath entries you need to separate them with a semicolon.
For ease of use you could create a startup script. This can be a simple batch script or a more elaborate ant script (which requires installing ant).
This is only the tip of the iceberg known as 'classpath hell'.
EDIT: you can also take a look at the Eclipse feature 'export runnable JAR', which packs your application together with all its dependencies in a JAR file.
How do I import libraries in my Java program without using an IDE, like NetBeans?
In NetBeans I do it this way:
How can I achieve the same thing by just using Notepad++ or Programmer's Notepad.
As much as possible I don't want to use NetBeans because it would be overkill since I'm only working on simple projects.
Use:
javac -classpath external.jar myClass.java
If your main class is in a package,
package com.mycompany;
public class myClass
{
...
...
then you'll need
javac -classpath external.jar com/mycompany/myClass.java
And to run:
java -classpath external.jar com.mycompany.myClass
In addition to Bala R's post, adding multiple files and locations is perfectly OK too...
javac -cp location1/;location2/;file1.jar;file2.jar fileToCompile
Notes:
-cp and -classpath are the same thing.
If you're on Solaris (and some other Unix flavors), change the ';' to ':'.
All of the other posters are spot on; you just need to add the JAR file to your classpath.
Java offers many mechanisms for setting the classpath, including via the command line, via an environment variable, and through setting it in the MANIFEST.MF of an executable Java JAR file.
These are all a pain in the neck to manage. It's good to know the technique, and understand the basics. But it's really a bad idea to actually use them.
You should do this.
First, put all of your Java libraries in a single place on your system. C:\java\libraries, or whatever. Someplace that you remember, and someplace accessible by all of your projects.
Next, name all of your libraries using their version numbers. If you using Log4j v1.4.1, then put the JAR file in a log4j-1.4.1 directory in your library area. This gives you "free" library versioning.
Finally, learn Ant. For simple projects, Ant is simple. Use the Ant build.xml file to compile, test, and run your application.
Why? Several reasons.
Because once it's set up, adding a new library to your project is trivial; you add a line to your build.xml file. Ant lets you more easily handle simple abstractions (like where all of your libraries are located).
The build.xml file is self-contained. If you use, say, an environment variable for the classpath, then the classpath for one project may be different from that of another. That means resetting the environment variable. Continue this and you'll end up swearing at some "new problem" where it "worked before" when it's because you had your classpath set wrong. Set it once in the build.xml file, and forget it.
Ant is portable. It runs the same on Windows, on Linux, on Mac, on AS/400, it runs everywhere that Java runs, unlike shells scripts or BAT files.
It's lightweight. Simple Ant scripts are simple. They don't bring a lot of baggage with them, and you can always make them scary complicated. It's much simpler than Maven for just builds.
Most IDEs support Ant directly. If you decided to go back to an IDE, most can simply use your Ant build file with minimal configuration.
This is how you solve your classpath problem with Notepad++. Setting the classpath works, but it doesn't go far enough. It's a pain to administer and manage. Learning the basics of Ant will take you much farther with minimal work.
You should put them on your classpath, like
java -classpath someJar.jar YourMainClass
And, of course, you can do the same for javac.
If you need to have more than one JAR file or directory on your classpath, you'll need to use your platform's default path separator. For example, on Windows,
java -classpath someJar.jar;myJar.jar YourMainClass
On a side note, you might find it easier to use an IDE to manage this sort of stuff. I've personally used just my slightly scriptable editor and have managed fine. But it's good to know how to do this stuff by the command line.
Put the JAR files in your classpath. classpath is an environment variable.
Make sure the JAR file is in your classpath and you have the import statement.
For my company, I'm making a batch script to go through and compile the latest revisions of code for our current project. I'm using Ant to build the class files, but encountered a strange error. One of the source files imports .* from a directory, where there are no files (only folders), and in fact, the folders needed are imported right after.
It compiles perfectly fine in Eclipse, but I'm using an Ant script to automate it outside of the IDE, and Javac throws an error when it encounters this line. Is there any automated procedure I can use to ignore/suppress this error with javac in Ant?
I'd even go so far as to create a dummy file in the importing directory, but all of that in contained in a Jar file I don't wish to have to decompress and then recompress with the dummy file.
Having an empty (package) directory would not cause an error.
Make sure the (root) directory of that package hierarchy is being added to the classpath specifed for javac.
eg. if the package is com.stuff and the directory is /java/src/com/stuff then you need to add /java/src to the javac classpath.
Or just remove the import, if it is importing .* from an empty directory then it is redundant.
What is the error?
Maybe this is out of the scope of your question but have you ever thought about Continuous Integration solutions? We use LuntBuild and are quite happy (other alternatives exist as well: CruiseControl, Hudson, QuickBuild).
Check to make sure you're using the same version of the JDK in both Eclipse and from Ant. Perhaps this is a difference across JDK versions?
The only other option would be that it's a difference in parameters being passed to javac.
I'm betting it's the former, not the latter.
For building Eclipse projects outside of Eclipse, have a look at the ant4eclipse project.