Start a class inside a jar file - java

I'm trying to run a simple piece of RMI code to be implemented in a larger project sooner. But when I put my server inside a jar and try to start it, I get an error; could not find class.
The command i used is java -cp myClassPath -Djava.rmi.server.codebase=file:C:\help\me\pls package-name.Logger
So I'm pretty sure my issue is with the very last part, how do i point to the class file insider a jar file?

But when I put my server inside a jar and try to start it, I get an error; could not find class. The command i used is java -cp myClassPath -Djava.rmi.server.codebase=file:C:\help\me\pls package-name.Logger.
There is nowhere here where you've tried to start a JAR file. You haven't even mentioned it on the command line.
The correct way to start a JAR file is to include a manifest with a Main- Class attribute, a Class-Path attribute if there are other JARs that this one depends on, and the command line
java -jar MyJar.jar -Djava.rmi.server.codebase=file:C:\help\me\pls

Related

NoClassDefFoundError when runnable 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"

Why do I need to delete the package line to run Java outside of NetBeans?

So I'm completely new to programming, and I've been writing some Java with the NetBeans IDE. My code runs fine within NetBeans, but I've tried to run it using the command line as well. However, if I run it from the command line, I have to delete the line:
package firstprogram;
which NetBeans automatically places at the top of each new file, or I get the error:
Error: Could not find or load main class FirstProgram
However, if I do delete the line, then the program no longer runs within NetBeans! It doesn't seem right that I have to choose whether to run a .java from within NetBeans or without.
The research I've done makes me think that this is something to do with directory structure? But everything I read on that goes straight over my head. NetBeans has a structure with "build", "dist", "nbproject", and "src", but when I use the command line I just place the .java file in an empty directory and javac from there.
Any explanation is appreciated! The books and tutorials I'm learning from either assume you're just using NetBeans or don't have the package line at all.
You can compile your class using javac command from anywhere, as long as you provide correct relative or absolute path. The problems come when you want to run your program using the java program.
You have to provide the correct path corresponding to your package declaration. For example, if I had 'MyClass' in package mypackage, first line would look like this:
package mypackage;
class source stored on disk:
c:/MyNetbeansProject/src/mypackage/MyClass.java
Compiled bytecode:
c:/MyNetbeansProject/build/classes/mypackage/MyClass.class
Now, if I would have opened a command prompt/terminal in folder c:/MyNetbeansProject/build/classes/, I could run the program using java mypackage/MyClass or java mypackage.MyClass.
However, if i would be somewhere else, I would have to say where the class files are located using the cp option: java -cp c:/MyNetbeansProject/build/classes mypackage/MyClass. The path in cp option can be relative or absolute, use "" when it contains spaces.
Package are directory architecture.
If your class is in the package com.acme.test, the class should be in the com/acme/test directory.
Instead of placing your class in an empty folder, place it in a folder named firstprogram and do javac firstprogram/youclass.java
The package (and folder) permit you to arrange your architecture with logical pattern.
More info here : http://www.tutorialspoint.com/java/java_packages.htm
So like OcterA said, you should keep organized, but with only one class this is not the issue. I believe that your problem is that you are not entering the correct command into the command line.
First cd to the correct directory and when you want to execute a file within a package in that directory you need to enter
java packageName.className
In this case
java firstprogram.FirstProgram

Compiling and excuting a Java class that uses Jar files

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!

Class paths with Java

I am trying to figure out how I may be able to setup my classpath file for java so that it could accept a jar and launch a class file not in the same directory as the one the user is currently in.
My code below shows java -cp .:jsoup-1.7.3.jar Class1 will allow me to launch the Class1 file without any problems if I am in the same directory as the Class1 file which also contains the jsoup file. However I am trying to launch it as if I were in another directory so I would try something like
java -cp .:jsoup-1.7.3.jar /pathtofile/Class1
However this doesn't work because you need to set the classpath correctly. How would I do this so I could do something like
java -cp {correct absolute class paths and arguments to include jsoup jar} Class1
Thank you in advanced.
"." means the current directory in Linux. so you'll need to substitute (or add) the path to where your .class file is.

running jar file from my class file in the IDE netbeans

I use a jar file called korat.jar. I executed with the command line:
java -cp $CLASSPATH korat.Korat --visualize --class test.Add --args 3
The classpath contains the path of the jar and also the Add.class file.
I want to execute this jar in my own program java in netbeans IDE. I think I would use:
String []s={test.Add.class.getName(),"--visualize","--class","test.Add","--args","3"};
Korat.main(s);
I get this exception: java.lang.NoClassDefFoundError
What do you mean "to execute the jar in my own program"? The jar contains some classes, if they are in your class path, you can instantiate the classes themselves and invoke some methods. In that case, you should use test.Add class. But it seems like the class is not in your classpath - java.lang.NoClassDefFoundError.

Categories

Resources