compiling package, including jars via command line - java

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.

Related

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.

Running a class that relies on Jar file in terminal

For my intro computer science class they recommend using eclipse as an IDE. I have used vim in the past and would prefer using it. There are two .jar files that the programs we create rely off of, because it is an intro class and we are not using java's main class functionality.
Right now we download the two .jar files and then use the Eclipse IDE build path function to link the .jar files with our code. Then when we run on Eclipse IDE it works perfectly fine.
How would I do this in ubuntu terminal? Thank you!
TLDR;
Intro comp sci class wants us to use eclipse, I want to use vim. How do you build path for a jar file to work with my class code in the ubuntu terminal.
Looked at this link and did not work
Java: how to import a jar file from command line
Update of image
You specify the jar files with a CLASSPATH, either using a CLASSPATH environment variable
export CLASSPATH="a.jar:b.jar"
java com.mypackage.MyClass
or on the command line with -cp like
java -cp a.jar:b.jar com.mypackage.MyClass
Include your jar dependencies while executing from command line.
java -cp tester.jar lab1

How to use Eclipse generated .classpath file to specify the external libraries?

I need to start a Java application using a .bat file . Here is what I have so far, and it works fine
java -cp ".;C:\someLibrary.jar;C:\someLibrary.jar;..." Main
The problem is there are too many external libraries, and Eclipse already generates a .classpath file referring to all those libraries. Is there any way I could use that Eclipse generated .classpath file in my batch, so that I don't have to list all the libraries in the java command above ?
Can I use something like this
java -cp ".;C:\ ..\pathToEclipseFolder.classpath" Main
The reason I am asking this is because I will eventually end up updating some of those external libraries. And I want to still be able to use the original .bat file
If your Java application needs the libraries, then you must specify them in the classpath of your .bat file.
A couple of considerations:
1) Maybe you have more libraries listed in your Eclipse classpath than you actually need
2) Java6 and higher allows you to specify a directory, instead of requiring you to specify each individual library in that directory:
java -classpath ".;c:\mylib\*" MyApp

How to use JFreeChart?

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.

Create Java App Install Package

First, I did look through the list of suggested questions generated by the keywords in my question but did not find anything relevant or helpful.
New to Java programming (not new to programming) so I don't know what useful tools there might be out there. I have a java console app, written using the intellij IDE. After testing and debugging, I am ready to deploy for a demo. Didn't find anything in the IDE that would let me do this!
I would now like to create a couple of installers - one for windows and one for linux. What do I do? I gather I just need the classfiles, but it would be nice to create an icon which would call the app with the right commandline options for the java.exe. As well, I have dependencies on log4j and jnetpcap (.dll requirements there)... how do I handle getting those support libs deployed - can I use the same installer or do I install them separately?
First things first, you need to JAR those class files. This is the standard way to package files in Java. A typical command would be:
jar cvf MyApp.jar *.class
Next you need to add a manifest to the JAR indicating the entry point into your program. Create a file called manifest.txt and add this line:
Main-Class: MyApp
MyApp would refer to the class name that contains the main() method. Now make the JAR again, this time specifying the manifest:
jar cvfm MyApp.jar manifest.txt *.class
On Windows, you can look into using Launch4J. You can use it to wrap your JAR in a EXE and specify that it runs as a non-GUI, console app.
In Linux, you can include a shell script along with your JAR to execute it. Place the script in your path. For example:
#!/bin/bash
java -jar MyApp.jar
It would be additional work to add dependent libraries to the mix as well as create installers. Seems too broad to include all in one question, but hopefully this will get you started.

Categories