This question already has an answer here:
Exporting executable/runnable jar in eclipse
(1 answer)
Closed 7 years ago.
I just wrote Java console project , which i shoud pack as jar file. I googled it alot and found different solutions like (Eclipse) Project -> New Configuration -> e.t.c Then export as jar file using speciefied config. That's nice, but it's not what i need. After making repeatedly - it won't work. I tried to do same as mentioned here: click
Second Solution: create jar using manifest. Well it's much better, because i can specifiy entry point of main.class. But it's won't work due to can;t find out where's annother packages.
Upd: Launcher.class looks like this
package backpack.dao;
public class Launcher{
public static void main(String[] args){
Backpack.start();
}
}
My project structure:
src/backpack/dao/Item.java
/Backpack.java
/Chromo.java
src/backpack/main/launcher/Launcher.java
The Question is: What should i write in manifest instead of this:
Main-Class src/backpack/main/launcher/Launcher to make executable jar
successfully?
P.S Launcher class uses instances from Backpack.java
Please don't downgrade. I'm rookie
Thanks
What is the package of the Launcher.java? Have you included package name to the classname in manifest.txt?
Remember, when Launcher.java is in package "backpack.main.launcher" your classname in manifest.txt will be:
Main-Class src/backpack.main.launcher.Launcher
instead of:
Main-Class src/backpack/main/launcher/Launcher
Also, remember about this:
The text file must end with a new line or carriage return.
To check what exactly is wrong with your jar file run it in the command line using:
java -jar MyJar.jar
and paste here output with error message.
For more help you can check this example from Oracle: link
Edit:
I recreated your project structure and I have found the solution (at least I think I have :) )
Compile your code and go to the parent directory with compiled code (with *.class files, this should be "out" directory or something like this - depending from your IDE). For example: when your project is in some/path/src/backpack/main/launcher go to src directory.
Run command:
jar cvfe MyJar.jar backpack.main.launcher.Launcher -C . .
(Yes, two dots at the end)
This should create jar with name MyJar.jar and manifest file (-e flag) with main class backpack.main.launcher.Launcher. -C flag include all *.class files recursively from directory . (current one), so both from dao and launcher package.
Test jar: java -jar MyJar.jar
Related
Been looking for around 2 hours for a solution to my problem, tried countless solutions and still the problem remains unsolved.
I have a game project with few packages; geometry, levels, decoration, and game. Inside the game package resides a gameRun.java
package game;
public class gameRun {
/**
* Program entry point.
* #param args the arguments for the levels
*/
public static void main(String args[]) {
... some code
}
}
and I have the following manifest file :
Main-Class: game.gameRun
Class-Path: someExternalGraphicTool.jar
//newline here
I compile my whole game using the following command : (the bin folder already exists).
javac -d bin src/geometry/*.java src/decoration/*.java src/levels/*.java src/game/*.java
I then turn my compiled classes into an executable .jar file using the command :
jar cvfm t.jar manifest.mf bin/geometry/*.class bin/decoration/*.class bin/levels/*.class bin/game/*.class
I then try to run my t.jar using :
java -jar t.jar
//or
java -cp .:t.jar game.gameRun
which both produce the error, "Error: Could not find or load main class game.gameRun"
However, when I unzip the jar file, inside the bin/game directory I can see a gameRun.class file and inside the META-INF\MANIFEST.MF file I can still see that the Main-Class is set to game.gameRun.
Note that I am working on a remote linux server, on a command line, and the jar file doesn't work even if I download it to my windows 10 machine.
What did I do wrong during this process ? Thanks for any help.
Your classes are in a wrong structure inside a jar file, because of the bin folder. My suggestion: pack it all into a jar starting in a bin folder.
Now when you extract your jar you will see 2 folders: META-INF and bin.
If you make the jar from bin folder you will see: META-INF and game, and it will work.
It doesn't work for you simply, because it can't find the main class, since it is inside the bin/game/YourClass.class, not in game/YourClass.class.
...\bin> jar cvfm t.jar manifest.mf geometry/*.class decoration/*.class levels/*.class game/*.class
And then just:
...\bin> java -jar t.jar
Hi Guys I have included the Webcam-Capture API in my project.
When I run it in Netbeans everything works fine. But when i compile everything to a runnable jar i get this message trying to run it by cmd line.
can anyone of you help me?
i already tried to unbound and rebound all jars and changing jdks but its not working
add -classpath flag in the command line ,pointing to the path where Webcam-Capture API exists in your file system, unless you want to create a single package executable.In your case It should be something like below
java -classpath YOURJAR.jar;folder_of_dependant_jar/*;. com.awesome.pagackage.Starter
Where YOURJAR.jar contains the com.awesome.pagackage.Starter.main(String args[])
You also mentioned that your jar is a runnable jar it also means that while exporting/building you can do one of the following way.( NOTE , this feature is in eclipse , but you would get the idea ).Each of the following options you see in the library handling does specific things.
The first option: Extracts the dependent jar into your target jar as java packaging.This means if your package is com.awesome.package and the dependent jar has package logic.package; , after the runnable jar is build you could find both these package exists in your jar file.
The second option: I think it is more on eclipse specific since eclipse adds few classes of its own , of runnable generation, so I am not explaining it here.
The third option : is the most interesting one. it creates folder stucture like below
ndon_lib\external.jar ( external jar file )
ndon.jar ( your jar file )
This time the manifest.mf file contains something like below.
Class-Path: . ndon_lib/external.jar
Main-Class: com.awesome.pagackage.Starter
You should set the classpath
java -cp "your.jar" "yourclass"
Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.
This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 9 years ago.
For years I have been using an IDE (Eclipse) to compile my jar files for me, through the years I have learned about how they work however I still don't fully understand how the jar knows where the main method is, I am also curious about how simple (or not) it is to compile one manually.
I have a (sort of) IDE I'm working on that will need to be able to compile and run a jar that includes both the file from the user and either a jar or a bunch of other classes (the API), I have seen some questions here mentioning Java JavaCompiler class but never giving demo code and there seems to be a next to no one that knows how to compile manually so I would like to contribute. So, how can I create a jar file using java code? Please provide demo code.
I still don't fully understand how the jar knows where the main method is
That's the job of the manifest file.
I am also curious about how simple (or not) it is to compile one manually.
It's pretty straightforward - you use the jar tool after you've built the class files.
Let's do a full walk through.
Create a directory called src and a directory called bin. Under src, create a directory demo and a file called Test.java in that directory:
package demo;
public class Test {
public static void main(String[] args) {
System.out.println("Working!");
}
}
Now compile the code:
javac -d bin src/demo/Test.java
(That will work on both Unix and Windows.)
Then create a manifest file called manifest.txt - it doesn't matter where it goes really, but I'll just keep it in src for the moment:
Main-Class: demo.Test
Now build a jar file:
jar cfm test.jar src\manifest.txt -C bin demo/Test.class
And run it:
java -jar test.jar
These days you can specify the entry point on the command line instead of building a manifest file yourself:
jar cfe test.jar demo.Test -C bin demo/Test.class
See the linked docs for more details on how to use the jar tool, and the potential contents of the manifest.
I'm new to working with Java from the command line and I don't get it. I read previous CLASSPATH questions but still didn't get my problem to work.
I have the following class in C:\Temp\a\b\c
package a.b.c;
public class Hello
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
The package name is intentional.
I compiled it fine and I put the Hello.class file inside C:\Temp\a\target
Now in the command line I go to C:\Temp\ and execute the following:
java -cp .\a\target a.b.c.Hello
It complains that it cannot find the class a.b.c.Hello
Thanks in advance!!
and I put the Hello.class file inside C:\Temp\a\target
This is wrong. It should be placed in the same folder as the .java file. The source code itself is declared to be in the package a.b.c; so, the .class file should really be kept in \a\b\c folder.
Then, to execute it just do:
C:\Temp>java -cp . a.b.c.Hello
Avoid "putting" the classfiles anywhere. The following should work:
javac -d c:\temp c:\temp\a\b\c\Hello.java
# creates Hello.class in c:\temp\a\b\c
java -cp c:\temp a.b.c.Hello
To expand on BalusC's point: the classpath defines a "root". When java is looking for your classes, it will start at each root (or jar) in your class path and drill down through the directories to match the package strucutre. You still need to have you class in a directory structure that matches its package name. In your case, to execute
java -cp .\a\target a.b.c.Hello
you would move the file to
.\a\target\a\b\c\Hello.class
Years ago, I too found this baffling.
Java will try to search for a directory structure a\b\c from starting in target and as you notice, it wont work.
Move the whole directory into target and you'll be fine, it should look like:
C:\Temp\a\target\a\b\c\Hello.class
You may compile it with the -d option which tall the compiler where to put the class file.
Many project structures are like this.
C:\whatever\projectname\src
C:\whatever\projectname\classes
C:\whatever\projectname\bin
C:\whatever\projectname\lib
C:\whatever\projectname\doc
That way you can always step on your project directory and type:
javac -d classes src\*.java
Which will compile all the sources in the src directory and will place them in the classes directory.
Then execute your program:
java -cp classes a.b.c.Hello
You may optionally place required jars in lib
This works pretty fine for small programs ( < 10 src files and 2 - 3 jar libraries ) If it grows beyond that, you could probably use an IDE or ant
The good thing about following this project structure is that some IDES ( as IntellJ idea ) just pick them very easily when you create a new project. You select "Create project from existing sources" and then you can continue from there.
I like compiling and editing at the command line a lot!!