Linux compile java with jar

How do I make a JAR from a .java file?

I was writing a simple program using a Java application (not application that has projects, but application within a project; .java) that has a single frame. Both of the files are .java so I can’t write a manifest needed by the JAR. The MyApp.java starts like a class with package, imports then public class MyApp and has a main function, but it’s still .java file! I’m writing it in JDeveloper 11g if it helps. Any ideas how to make a JAR from these files?

Agree with Chuck. Take a look at ANT. IDEs are great for developing, but you need to understand how things works. java files are compiled via javac to .class files, after this, .class files can be packaged into a jar with the jar command. (Ant has tasks for doing this, even jdeveloper has some wizard for doing this).

JDeveloper itself should have the capability to create jar files. A quick google search comes up with a howto here: tompeez.wordpress.com/2011/06/01/…

8 Answers 8

Go to the directory where you have your .java files

Run java compilation from the command line

if there are no errors, in the build directory you should have your class tree

move to the build directory and do a

For adding manifest check jar command line switches

I’ve tried compiling from the command line, but I get an error: Frame1.java:23: package oracle.jdeveloper.layout does not exist How do I solve that? Sorry for the newbie questions.

you have to build the classpath referring all the libraries you’re referencing in your classes. Check with your jdeveloper installation, there should be some ant build example (build.xml file) and at least a couple of good tutorials on how to use jDeveloper with ANT on Oracle’s site.

I’ve added an option to my jar command to automatically add the Main-Class to manifest. jar cfe Main.jar Main * (I’ve got confused though, I’ve had to put the Main-Class name after the output file name.)

Читайте также:  Connect to l2tp vpn linux

I’d like to point out: You have to move to build directory . If you try jar cvf YourJar.jar ./build/* , you will have trouble running your jar file later.

javac MyApp.java jar -cf myJar.jar MyApp.class 

Sure IDEs avoid using command line terminal

The above worked, however I needed to specify the .class of myApp. So it was jar -cf myJar.jar myApp.class

Ok this is the solution I would have liked to find, instead here I write it:

First create the directory structure corresponding to the package defined for the .java file, if it is my.super.application create the directory my and inside it super and inside it the .java file App.java

javac -cp /path/to/lib1.jar:/path/to/lib2.jar path/to/my/super/App.java 

Notice the above will include multiple libraries, if under windows use «,» to separate multiple files otherwise under GNU/Linux use «:» To create a jar file

jar -cvfe App.jar App my/app/ 

the above will create the application with its corresponding Manifest indicating the App as the main class.

Including the required libraries inside the jar file is not possible using java or jar command line parameters.

  1. manually extract libraries to the root folder of the jar file
  2. use an IDE such as Netbeans and insert a rule inside post-jar section of nbproject/build-impl.xml to extract the libraries inside the jar. See below.
        

the file.reference names are found inside project.properties file after you added the libraries to the Netbeans IDE.

Источник

How to compile java project with external jar file in Linux terminal

Question: I have project which includes external jar file in it, I followed this link http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29 to add external java path. What i’ve tried I created a .desktop File which executes a .sh file.

How to compile java project with external jar file in Linux terminal

I have project which includes external jar file in it, I followed this link http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29 to add external java path. Then I tried to compile my code in terminal, however I am still get an error about jar file does not exist.

I wrote the following commands: (Currently I am in the project directory and there are three folders called bin src and lib in there)

bash-3.2$ ls bin lib README.txt src bash-3.2$ javac -cp lib/jsoup-1.6.1.jar src/DayTradingStockBlog.java bash-3.2$ java -cp .:lib/jsoup-1.6.1.jar src/DayTradingStockBlog Exception in thread "main" java.lang.NoClassDefFoundError: src/DayTradingStockBlog (wrong name: DayTradingStockBlog) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) at java.lang.ClassLoader.defineClass(ClassLoader.java:616) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: src/DayTradingStockBlog. Program will exit. 

How should I solve this problem ?

You never use slashes, which are path delimiters, in a call to java (but to javac). If src is part of your package declaration — in this case the whole package declaration, which I bet it is not, you would, instead of:

 java -cp .:lib/jsoup-1.6.1.jar src/DayTradingStockBlog 
 java -cp .:lib/jsoup-1.6.1.jar src.DayTradingStockBlog 

But I guess it is just the place where you created the class, so the path belongs to the classpath:

 java -cp .:lib/jsoup-1.6.1.jar:./src DayTradingStockBlog 

You aren’t free to omit the path from the Class name, and append it to the classpath, or vice versa — it has to fit to your package declaration.

If you declare a package foo , (which has much more sense than src), your class name is no longer DayTradingStockBlog but foo.DayTradingStockBlog .

Based on your edit, I’d suggest you to enclose all classpath locations within single/double quotes. That is, make it like

java -cp ‘.:lib/jsoup-1.6.1.jar’ src/myClass .

In linux, the items in the classpath are separated by a colon (:) and in Windows, it’s a semicolon (;).

javac -cp %YOUR_JAR_LOCATION% myClass.java 

you should place the java file and jar file in the same dir example: javac -cp jdbc.jar myClass.java it works for me

How to compile and run a runnable JAR using only javac and jar, You also need a META-INF/MANIFEST.MF file to be able to run with java -jar yourfile.jar That»s the definition of «executable jar». You main

How to Create JAR(Java ARchive) File using Command line

JAR is a compressed file which uses ZIP format for compressing multiple class files into single
Duration: 4:06

Simple Step-by-Step Command Line Example of How to Compile

How do I use Bash to tell Java to run a .jar program in a specific Linux directory?

I am trying to write a Bash script to (amongst other things) run a particular Java programme stored in a .jar file.

The script is in a directory we will call foo . It includes this line

java -jar ~/Simutrans-Extended/simutrans-extended/Nightly-Updater-V2.jar -cl 

When I run the script, the .jar file executes as though it were running in foo . But I need it to be executed in its actual location, in this case ~/Simutrans-Extended/simutrans-extended/ . How do I do this please?

The script is running on Ubuntu 20.04, should that be relevant.

Use cd to change the directory in the line before the command. So the Bash script now reads:

cd ~/Simutrans-Extended/simutrans-extended/ && java -jar ./Nightly-Updater-V2.jar -cl 

How to convert «jar» to Linux executable file?, For a bash script, like Batch Script on Windows, we would start the file with #!/bin/bash . The path after the #! (hashbang) tells bash were to

Is there a way to execute a .jar file through a shell script? (Raspbian)

In gerneral

Mission

I want to run java -jar -Xmx2G -Xms2G /home/pi/minecraft/server/spigot-1.15.2.jar by clicking a desktop icon (or .desktop File).
When I type the commant into a terminal it works.

Problem

What i’ve tried

I created a .desktop File which executes a .sh file.
My .desktop File:

[Desktop Entry] Version=1.0 Name=Spigot Comment= Exec=/home/pi/Desktop/launch_spigot_server.sh Icon=/home/pi/Pictures/spigot.png Terminal=true Type=Application Categories=Utility;Application; X-KeepTerminal=true 
#!/bin/bash pkexec java -jar -Xmx2G -Xms2G /home/pi/minecraft/server/spigot-1.15.2.jar 

(«pkexec» because I think I need sudo rights to run a .jar file)

Result

The Terminal actually opens and executes the .jar file and it says Loading libraries, please wait. , then throws some sort of Error and instantly crashes.

That is the farthest i’ve come in 3 hours .

First go to the directory and then execute the java -jar. command

#!/bin/bash cd /home/pi/minecraft/server/ java -jar -Xmx2G -Xms2G spigot-1.15.2.jar 

How can I make jar in linux?, You can use command line utility named jar e.g.. jar cvf classes.jar .*.class. Use online help of jar for more info. Since jar is a part of

Источник

Compile and Run Java in Command Line with External Jars

The following example shows how to compile and run Java program in command line mode with external jars. It is developed under Linux.

1. Compile & Run Java Program Without External Jar

Let’s create a simple hello world program «helloworld.java».

public class helloworld{ public static void main(String[] args){ System.out.println("Hello!"); } }
$ javac helloworld.java $ java helloworld

2. Compile & Run Java Program With External Jar

Now let’s download a third-party library and use some method from the library. In this case, I downloaded apache.commons.lang from here, and use the StringUtils.capitalize() method. The jar file is downloaded to «jars» directory which is located the same with helloworld.java.

import org.apache.commons.lang3.*; public class helloworld{ public static void main(String[] args){ String x = "abcd"; System.out.println(StringUtils.capitalize(x)); } }
$ javac -cp ". /jars/common.jar" helloworld.java $ java -cp ". /jars/common.jar" helloworld

For Windows, «:» should be replaced with «;».

If you want someone to read your code, please put the code inside

 and 

tags. For example:

Источник

Оцените статью
Adblock
detector