Running a class that relies on Jar file in terminal - java

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

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.

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.

How to add a jar file to an eclipse java project from command line?

I want to compile an eclipse java project from command line. To make it easier I've put all my java files and the jar file that I need in my program in one direcotry. The path to the said direcory is:
C:\Users\Besi\Desktop\Test\src
My main class is called Program.java
Now, I am having problems with importing the jar file and compiling my main class. I mean I cannot compile it without importing the jar file but at the same time I don't know how to this in the command line. I tried this:
javac -classpath ".:C:\Users\Besi\Desktop\Test\src\javatuples-1.2;" Program.java
but it doesn't work. Any advice?
Assuming this is your end goal (and not simply trying to "add a jar file to an Eclipse java project from the command line" as your actual question title states):
I want to compile an eclipse java project from command line.
Turns out that you can compile your Eclipse project from the command line without having to place your .jar files and .java files in the same directory.
I just did it (and ensured I had two .jar files in my Eclipse project) using the following command as executed from the workspace directory (which I modified using the information from your question, assuming C:\Users\Besi\Desktop\Test is the workspace of your project, and assuming you're running Windows):
eclipsec.exe -noSplash -data "C:\Users\Besi\Desktop\Test" -application org.eclipse.jdt.apt.core.aptBuild
Because I cannot yet comment, I am posting this as an answer, even though I adapted this answer based on the contents of a previous SO answer. Please edit accordingly if this causes any trouble, thanks.

Running jar file in command prompt. Java can't find libraries in classpath

EDIT: OK, I think I was being silly. What I really wanted to do was a runnable JAR. I did and now it works.
So I'm trying to run a little program that interacts with some webpages. The program works fine when I launch it from Eclipse but when I export it to a jar file and try to run it from the command prompt I get this error message:
What am I missing here?
I'm assuming in Eclipse you've added the appropriate libraries to your build path. When you run your program through eclipse, it automatically creates a java command including all your libraries in the classpath.
You need to do the same thing
java -cp /path/to/libs -jar muzictest.jar

Distributing a Java console program

I have created a Java console application using Netbeans. In the Netbeans dist directory I have the class file of the project. Now I need to give the executable files to someone else
who will run them on another PC.
Which file I should send? How can he run them on his PC? Is there any way to create an exe type file?
Both PCs have the JDK installed.
Build a jar file with a main class specified in it.
If he has Java installed and .jar is associated with that, he should be able to just double-click on it.
Alternatively from a command line he'd be able to run:
java -jar program.jar
There are programs around to create executable wrappers around this, but a jar file is a simpler solution in terms of packaging - it's worth trying that to start with.
In additon to Jons answer:
If you have a runnable jar to start with, it is frequently much easier to package it up in an EXE file. If you have the need search Stackoverflow for JSmooth and one-jar.
NetBeans actually answers your question. If I do a Clean/Build, the output says:
To run this application from the command line without Ant, try:
java -jar "insert_your_project_name_here.jar"
theres a handy page about this here:
java tools tutorial
It describes creating jar files a little further down the page
You can go to run menu and select clean and build project or shift+f11 after this go to dist folder in project folder and use .jar file and run that by hint say in over .

Categories