Having trouble with java packages/setting classpath - java

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

Related

"Could not find or load main class" command prompt

I know this question has been asked loads of times before, but I'm a rookie programmer and despite trying many of the solutions on this site I still can't fix this issue. I'll be really thankful if you can take the time to figure out what I've done wrong.
Operating system: Windows 8
Java version: 1.8.0 update 25
The command prompt I'm using is the one that comes with Windows. (I'm presuming there are other types so I'm just making it clearer.) The code's a really basic one.
package com.thefinshark.intro;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome.");
}
}
So, first I changed the directory to C:\javawork, where Welcome.java is saved. I set the path to C:\Program Files\Java\jdk1.8.0_25\bin, then compiled the code. The compilation seemed fine, I found the Welcome.class file in the C:\javawork as well. The execution, however, kept returning "Could not find or load main class Welcome". I've tried C:\javawork>java Welcome and C:\javawork>java com.thefinshark.intro.Welcome, and loads of other variations. I've also changed the classpath to C:\ and C:\javawork but it still dosen't work. Someone answering a similar question suggested adding dt.jar and tools.jar to the classpath but no dice.
It'll be great if someone could help, and I'll be happy to help pass on the information to the others who have problems like this as well. (As I'm typing this I'm looking at a whole long list of similar questions.)
The directory structure must match the package name of your source file. So, if your class is in the package com.thefinshark.intro, then your source file must be in a directory com\thefinshark\intro.
So, for example, you should save your source file as C:\javawork\com\thefinshark\intro\Welcome.java, and then compile and run it from the directory C:\javawork:
C:\javawork> javac com\thefinshark\intro\Welcome.java
C:\javawork> java com.thefinshark.intro.Welcome
Note: The javac command expects a filename of the source file you are compiling (com\thefinshark\intro\Welcome.java), and the java command expects a fully-qualified class name (com.thefinshark.intro.Welcome).
See Lesson: Packages for more details on how to work with packages.

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!

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.

Java package issue

I am trying to import a package found at /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar. I am using import ptolemy.plot.* and compiling with javac -cp /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar The Class.java. When I run (using java -cp ...same... TheClass) I get Error:Could not find or create main class TheClass.
When I take away the -cp from the java call the Could not find or create error goes away...
I know this must be something simple but I can't figure this out!
If you specify that the classpath is a single jar files -- as you seem to be doing -- then Java won't find any classes outside of that jar file. You need your classpath to include both the jar file and the location of your compiled classes. You can use "." to mean the current directory; i.e.,
java -cp .:/home/jirwin/ptplot5.8/ptolemy/plot/plot.jar TheClass
Note the "dot colon" prepended to the beginning of the classpath.
You need to put your full package name in front of your .class when you run it with java. Otherwise it looks in the wrong place, or something. (I don't understand java well enough to give you the "why" but it's the idea.)
java -cp /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar {package}.TheClass

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