Compiling and excuting a Java class that uses Jar files - java

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!

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"

NoClassDefFoundError when attempting to run from command line

Apologies in advance - I know there are many, many, many very similar questions but I wanted to ask something specific to my situation.
I have a bunch of java and jar files in the same directory. I am able to compile fine and thus end up with a number of class files in the same dir. But when I go to execute the program it gives a NoClassDefFoundError saying it can't find the specified class:
C:\Users\DB\Desktop\nextreports-integration-demo\src\ro\nextreports\integration>
java -cp ".;*.jar" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)
I tried the same thing from a higher-level dir but it made no difference:
C:\Users\DB\Desktop\nextreports-integration-demo\src>java -cp ".\ro\nextreports\
integration\*.jar;.\ro\nextreports\integration" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)
The package statement in the source file is:
package ro.nextreports.integration;
I have a feeling I'm overlooking something very elementary. Thanks in advance.
Edit: Thanks very much. It works with the following:
java -cp ".\ro\nextreports\integration\nextreports-engine-6.3.jar;.\ro\nextreports\integration\commons-jexl-2.1.1.jar;.\ro\nextreports\integration\commons-logging-1.1.1.jar;.\ro\nextreports\integration\derby-10.10.1.1.jar;.\ro\nextreports\integration\itext-2.1.7.jar;.\ro\nextreports\integration\itext-rtf-2.1.7.jar;.\ro\nextreports\integration\itextpdf-5.0.6.jar;.\ro\nextreports\integration\jcalendar-1.3.2.jar;.\ro\nextreports\integration\jcommon-1.0.15.jar;.\ro\nextreports\integration\jfreechart-1.0.12.jar;.\ro\nextreports\integration\jofc2-1.0.1.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\poi-3.7.jar;.\ro\nextreports\integration\winstone-lite-0.9.10.jar;.\ro\nextreports\integration\xstream-1.3.1.jar;" ro.nextreports.integration.SimpleDemo
But why can I not use wildcards for the *.jar files? For instance, the following leads to a NoClassDefFoundError for a class in any jar file I don't make explicit:
java -cp ".;.\ro\nextreports\integration\*.jar" ro.nextreports.integration.
SimpleDemo
Suppose this is your directory tree:
src/
ro/
nextreports/
integration/
SimpleDemo.class
From your package declaration, your compiled class should be in the integration subdiretory. Check that there really is a SimpleDemo.class in that directory. If that is correct, its classpath includes the contents of the src directory.
That means that, if you didn't have any JAR dependencies, you could run your application from the src directory like this:
java -cp . ro.nextreports.integration.SimpleDemo
You need to use the fully qualified class name.
Since you do have jars, you have to include them in the classpath as well. Suppose you have one JAR in the directory above src, and another in the current directory, you could use:
java -cp ../one.jar;another.jar;. ro.nextreports.integration.SimpleDemo
If you run it from another directory, it will still work if you review the classpath's relative directories or use absolute directories to describe your classpath. I am not sure but usually the current directory is always included in the classpath by the java executable.

Having trouble compiling a Java program, I am new to it. Can't figure out what to do with directories

I have been searching the web trying to find the answer to my question, but everywhere I look seems to have too complex of a solution for a beginner like me. I have been working on this project, and just now realized that I should've made a package, or something like that. The thing is though, my program was working fine until I started dabbling with it, and now it won't work at all. I am getting this error:
Exception in thread "main" java.lang.NoClassDefFoundError: BubbleSort. class
Caused by: java.lang.ClassNotFoundException: BubbleSort.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:672)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at ``java.lang.ClassLoader.loadClass(ClassLoader.java:638)
Could not find the main class: BubbleSort.class. Program will exit.
Here's how my "path" looks, if I am not mistaken. I am connected to my school's Z: drive through a remote connection, and from there I have a folder called myFirstname_Lastname_A4,
which then leads me to another folder called sortingzz which I believe is supposed to have only my source files, but it also ended up with my class files in there whenever I compiled. So here's what I am doing to compile.
cd myFirstname_Lastname_A4/sortingzz
javac *.java (Works fine, this is where I end up with my Class files inside of my sortingzz folder)
java * (This is where I get the error)
I am pretty sure I am just trying to run the program wrong. Before I started messing around with stuff I wasn't ready for, I used to just run the file with my main function in it, like this
javac SortingImplementation.java
java SortingImplementation
And that for the most part worked fine, but I started having trouble calling certain classes from other classes, so thats when I found out I was suppose to do the packaging and importing stuff.
In case that is the issue, I have on the top line of every source file:
package sortingzz;
and I am importing like this:
import sortingzz.*;
This is correct, right?
UPDATE:
I decided to give up on class pathing and trying to package everything, because as usual, I am getting responses that are way over my head, and to be honest I don't think it is necessary.
After removing package and importing from everything, and once again compiling using javac *., it actually compiles this time. However whenever I try to run my class with the main in it, SortingImplementation, it tells me that
Could not find the main class: SortingImplementation. Program will exit.
I don't get it, I am looking at the SortingImplementation.class right now, with all the other classes and java files, so I am not sure what it's trying to do.
javac *.java is fine. This will compile your files. However, you only need to run the file with your main method in it: java MainClass
You say that you are using packages to organize the classes. In this case you need to set the class path using the -cp flag.
javac -cp /path/to/parent/of/package classname.java
and
java classname
Also, your main class should be declared public and should have a main()
NoClassDefFoundError occures when a class was recognised in compile time but was not available during runtime.
So the JVM can't find your class in the classpath.
using -cp flag to specify where your package is should work.
the commanc javac *.java compiles all found java files to corresponding .class files. If you all your classfiles are in the same folder, which they should, you just run your regular java SortingImplementation command.
java * would, a bit depending on your OS, yield in an undesired command. For instance, on Linux it would be expanded by the OS to java SortingImplementation.java SortingImplementation.class BubbleSort. The last one is a directory, which ofcourse is not an executable class.

Having trouble with java packages/setting classpath

I usually don't have any problems setting up the classpath and running programs, but I'm running into a bit of a problem. I'm working on a program that will download a series of reports. If the working directory is called Report downloader, my project resides in
src/org/report/reportdownloader
and the jar files I'm working with reside in
lib/
When I'm going to compile my project (I'm in windows :( ) I type in
javac -classpath .;..\..\..\..\..\lib.transfer.jar; ..\..\..\..\..\lib.someotherjar.jar; ReportGrabber.java ReportDriver.java
I get an error message saying
ReportDriver.java:12: error:package com.transfer does not exist
import com.transfer.*;
^
1 error
And I don't really understand why. I'm trying to import a valid package, and I showed it where to find the jar in the classpath and it's still giving me grief.
I'm losing my mind, I feel so dumb for asking about this. I could give up and just use eclipse but I really want to figure this out.
EDIT: When I type
java -cp .;..\..\..\..\lib\transfer.jar; ..\..\..\..\lib\someotherjar.jar; ReportDriver
to run the file, I get an error saying
Error: could not find or load main class ..\..\..\..\lib\someotherjar.jar;
Any ideas?
Why are there 5 .. instead of 4?
to access your lib directory from reportdownloader, you have to do
..\..\..\..\lib
not
..\..\..\..\..\lib
Using a relative path seems like a bad idea to me.
Why not do this:
... -classpath /lib/transfer.jar /lib/someother.jar
or in windows:
... -classpath c:\lib\transfer.jar c:\lib\someother.jar

Could not find or load main class

I have Windows 7, installed jdk1.7.0 and its supporting jre7.
My problem is compilation part works perfectly, but while running the Java program I get this error saying:
"Could not find or load main class"
I am storing all my programs in javalab folder. I have set the path to it. Procedure looks like this:
C:\Users\user>cd\
C:\>cd javalab
C:\javalab>autoexec.bat
C:\javalab>set path=C:\Program Files\Java\jdk1.7.0\bin
C:\javalab>javac p1.java
C:\javalab>java p1
Error: Could not find or load main class p1
C:\javalab>
I was having a similar issue with my very first java program.
I was issuing this command
java HelloWorld.class
Which resulted in the same error.
Turns out you need to exclude the .class
java HelloWorld
Try:
java -cp . p1
This worked for me when I had the same problem, using Fedora (linux)
Simple way to compile and execute java file.(HelloWorld.java doesn't includes any package)
set path="C:\Program Files (x86)\Java\jdk1.7.0_45\bin"
javac "HelloWorld.java"
java -cp . HelloWorld
pause
javac should know where to search for classes. Try this:
javac -cp . p1.java
You shouldn't need to specify classpath. Are you sure the file p1.java exists?
I had almost the same problem, but with the following variation:
I've imported a ready-to-use maven project into Eclipse IDE from PC1 (project was working there perfectly) to another PC2
when was trying to run the project on PC 2 got the same error "Could not find or load main class"
I've checked PATH variable (it had many values in my case) and added JAVA_HOME variable (in my case it was JAVA_HOME = C:\Program Files\Java\jdk1.7.0_03)
After restarting Ecplise it still didn't work
I've tried to run simple HelloWorld.java on PC2 (in another project) - it worked
So I've added HelloWorld class to the imported recently project, executed it there and - huh - my main class in that project started to run normally also.
That's quite odd behavour, I cannot completely understand it.
Hope It'll help somebody. too.
i guess that you have a different class name in p1.java
Check you class name first. It should be p1 as per your batch file instruction. And then check you package of that class, if it is inside any package, specify when you run.
If package is x.y
java x.y.p1
Here is my working env path variables after much troubleshooting
CLASSPATH
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
PATH <---sometimes this PATH fills up with too many paths and you can't add a path(which was my case!)
bunchofpaths;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
Additionally, when you try to use the cmd to execute the file...make sure your in the local directory as the file your trying to execute (which you did.)
Just a little checklist for people that have this problem still.
I've had similar problems. If you work with Eclipse, you need to go to the folder where you have your src/ folder... If you used a package - then you use
javac -cp . packageName/className
which means if you've had a package named def and main class with name TextFrame.java you'd write
javac -cp . def/TextFrame
omitting the trailing .java extension, and then you run it with the
java def/TextFrame
and if you have have arguments, then you need to supply it with arguments corresponding to your program.
I hope this helps a bit.
First, put your file *.class (e.g Hello.class) into 1 folder (e.g C:\java). Then you try command and type cd /d C:\java. Now you can type "java Hello" !
You might have the CLASSPATH environment variable already added!!
Use following to avoid further usage of -cp . in java -cp . CLASSFILE
Add . to CLASSPATH in system properties->environment variables or by cmd
set CLASSPATH=%CLASSPATH%;.;
I faced a similar problem in Eclipse. Whenever I clicked on the Run button it gave me the message, "Error: Could not find or load main class". But when I right click on the java file in the project explorer and Run As Java configuration, it works perfectly.
I think this is because it tries by default to run it in some other configuration which causes problems.
Hope this answer helps some.
If you have a single .java file to compile using command-line , then remove
topmost package parts from the code, the compile again, it will work.
This worked for me.
Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the:
Could not find or load main class xxx Linux
I just delete that reference and added again and it worked fine again.
i had
':'
in my project name e.g 'HKUSTx:part-2'
renaming it 'HKUSTx-part-2' worked for me
You can use NetBeans IDE which is free to download and use "Open Source". You can even do other programming language in this IDE. The latest of which it supports HTML5. This makes your programming easier. If you're not familiar with it choose a book that is NetBeans integrated like Sams Teach Yourself Java in 24 Hours

Categories

Resources