How to use JFreeChart? - java

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.

Related

compiling package, including jars via command line

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.

How do I compile Java code that uses JAR?

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.

How to compile and execute this JAVA application in Ubuntu?

There is a Hdfs.java inside src/hdfs and there is a utility jar file inside lib.
What are the options I give when I compile the Hdfs.java using the utility jar?
And how do I execute?
I went through the examples here and I'm very confused.
If you do not want to use the compiler inside of NetBeans, then go to your terminal application, set your current directory to src and invoke
javac -cp .:../lib/utility.jar Hdfs.java
To run, you would do this:
java -cp .:../lib/utility.jar Hdfs
If you want to compile and run within NetBeans, see Jigar Joshi's answer.
Looks like you are using net beans, right click on project and do clean and build

Editing Java files outside of project in Eclipse

I have to write some little programs in Java for school, so I don't want all that stuff that Eclipse generates with a new project. The way I'm doing it now is this:
$ touch myprog.java
open and edit myprog.java in Eclipse
$ javac myprog.java
$ java MyProgClass
The problem is that Eclipse doesn't show warnings and errors while typing the code. It would also be nice if it would let me compile and run the file inside Eclipse (by doing what I do above in the working directory).
Is there any way I can make Eclipse do this?
This is because Eclipse doesn't consider your .java file as something it should compile (and thus generate errors for)
You need to mark the folder containing myprog.java as a source folder. Here's how you do it:
Right-click on the folder and choose "Build Path" -> "Use as source folder".
Well because now it's just a text file for eclipse and it isn't linked to java project.
Create new project and put myprog.java to /src folder in eclipse project.
Then:
$ javac workspace/yourpoject/src/myprog.java
$ java workspace/yourproject/bin/myprog
As a result copy only those 2 files and ignore the rest of project files if you want.
Can't you use an existing project into which to create school classes?
Alternatively the NetBeans IDE is somewhat slimmer. You would use Run File there.
The short answer: no, you can't make eclipse do that.
Eclipse JDT needs to know the classpath to compile (even if that classpath only includes the JRE), builders to tell you if there are errors, and the search engine for standard IDE things like content assist or open declaration.
It's trivial to create one java project, and then use that to create all of your little java programs. They compile correctly, report errors, and are easy to run ... and if you want to run them from the command line as well, there's nothing stopping you.

Compiling java from the command line

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.

Categories