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"
Related
Below is my sample class file:
package org.foo.tutorial;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
In order to execute the project (maven framework) we run:
>java -cp Something-1.0.SNAPSHOT.jar org.foo.tutorial.APP
The above command works fine and gives me the output 'HELLO WORLD'.
However, if I leave out the third argument in the above command (org.foo.tutorial.APP) I get the following error:
Error: Could not find or load main class target.MavenTutorialApp-1.0-SNAPSHOT.jar
My question is:
Why should the groupId and app name matter when I am supplying the entire 'jar' file ?
The error is a bit misleading. Your java command is incorrect since you don't specify a class. The Something-1.0.SNAPSHOT.jar is meant to be part of the -cp option but java is interpreting it as the class.
That's how java behaves
The java command starts a Java application. It does this by starting a
Java runtime environment, loading a specified class, and calling that
class's main method.
If your .jar file contains an entry point specified by a Main-Class header in the manifest, then you can simply run
java -jar Something-1.0.SNAPSHOT.jar
Let me try to answer your question.
Why should the groupId and app name matter when I am supplying the
entire 'jar' file ?
Lets divide the question into smaller part -
What is group id? - The id of the project's group.
What is artifactId? - The id of the artifact (project) and off course version is part of default artifact name.
While running a java program under jvm there are no such effect that how you built jar, for example it is produced by maven or gradle build process or even command line.
Things that matter is jar file and entry point or the class where main reside.
This may be out of scope of this question, but i felt relevant.
To run java program from jar file, you can explicitly mention like above. Also you can create executable jar file by adding manifest file, where it define the entry point of the executable -
Main-Class: org.foo.tutorial.APP
How to create executable jar?
Also maven maven-assembly-plugin helps to package executable jar.
help
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
I'm trying to use R to hook the Java code from the GSRad project. The GSRad Java code is available online and comes as a One-Jar project jar (I was not familiar with One-Jar until today). I can run the One-Jar file just dandy using the following command (after unzipping the file from the above link):
java -jar gsrad_sample.jar
When I pop open the gsrad_sample.jar file I see a jar titled clima_GSRAD-1.0.0.jar in the /lib/ directory which contains the class files I want to hook with R. I've pulled out the jar of my affection and tried the following, to no avail:
library(rJava)
.jinit()
.jaddClassPath( "/home/jal/Documents/DSSAT/gsrad/clima_GSRAD-1.0.0.jar" )
.jnew( "cra/clima/gsrad/GSRBristowCampbellStrategy" )
Any tips on how I might hook the classes inside the clima_GSRAD-1.0.0.jar? I'm flummoxed.
EDIT
The GSRad site requires registration which is annoying. The full zip file which contains the Doxygen documentation for the Java package as well as the One-Jar jar file is available here and if you pop that open the jar that has the classes I want to hook is this one.
Let me preface my answer by saying that I'm no expert in Java / rJava, so apologies if this isn't 100% correct. I hope it's a step in the right direction though.
Start by unzipping gsrad_sample.jar to C:/gsrad (or adjust your paths based on where you unzip it). Then add all the contents of C:/gsrad/lib to your class path:
library(rJava)
.jinit()
.jaddClassPath(dir( "C:/gsrad/lib", full.names=TRUE ))
.jclassPath()
.jnew( "cra/clima/gsrad/GSRBristowCampbellStrategy" )
I'm new to using jar files on my applications so here is my problem.
I wrote my code on net beans and added into the library the jar file I need, which is:
poi-3.10-FINAL. The program runs perfectly from net-beans, however when i try to run it from the command line seems like it doesn't find some of the files inside the jar. Reason for this I would like to make it an executable after i get this solved.
In the command line I'm compiling my code as follows:
C:\Users\chuser10\Desktop\Excel\src\excel>javac *.java -cp C:\Users\chuser10\Des
ktop\Excel\src\excel\lib\poi-3.10-FINAL.jar
It compiles perfectly, which lead me to think everything is good to go, however this is not so. I tried then running my main as ...>java GUI and i got this:
C:\Users\chuser10\Desktop\Excel\src\excel>java GUI
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apach
e/poi/poifs/filesystem/POIFSFileSystem
I checked inside the jar and the file is there. Any clue why this might be?
You need to specify the classpath when you run the program too. Compiling doesn't link the library into your code as happens in C and many other languages; in Java linking happens at runtime.
Probably -cp .;C:\Users\chuser10\Desktop\Excel\src\excel\lib\poi-3.10-FINAL.jar will be what you need. The '.' at the beginning means the current directory, which is where the class files that constitute your program are rooted. The ';' is just a separator.
Got it working. You are both correct, we have to specify the class-path at runtime as well.
I went ahead and went for the executable jar file creation and put it on my manifest:
..>jar cfm < *.class>
On my manifest:
Class-Path: poi-3.10-FINAL.jar
Main-class: GUI
Cheers!
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!!