java.lang.NoClassDefFoundError in cmd - java

I am facing this problem while trying to run my java file by writing java filename ....
I have read on many pages the possible ways this could be corrected but unfortunately I have been unable to correct my problem...
First of all I looked at my environment variables and observed that there was no CLASSPATH set and I had pointed PATH correctly to my jre as well as jdk bin in C:\
Second I am able to run javac filename.java and observe that .class file gets built in the local directory.
While writing javac -classpath . filename works writing java -classpath . filename (without .class) results in the same error.
I just don't know how to run my program in command prompt!!!!
Please do not give me links to the pages which have given the same answers that I have mentioned above as they do not work in my case.....
Please help....

Note that if your class resides in some package mypackage, you need to make sure the class file is inside mypackage/ and do
java -classpath . mypackage.YourClass

There is a bit little information in your post ... as said, a complete example would really help solving this. Some ideas:
Is class named filename? If not, you should make sure to call the right class name in the java command.
if you have strange characters in your class name (basically anything apart from A-Z, a-z, 0-9, _), it could be mangled by your file system and thus lead to java not being able to find it. Make sure that the class name is the same as the file name.
If you use packages, make sure the package names are congruent to your file structure (see latest questions with tag package for some examples).
Is your class public? If not, make it so. (This should give another error, though.)

Stating javac filename.java and java filename are not enough for this really.
We also need to know the contents of the filename.java or at least the first few lines.
This is the expected way to declare a class
public class Filename {
}
This is the way it would have to be to work given your example: java filename
public class filename {
}
This is could exist too, but you are probably just messing with us :)
public class SomethingElse {
}
Overall no matter what the filename is on the filesystem, the class has a name and that is the name that the java command is expecting. I would recommend using the upper case first letter form as it is clearer imo

Related

How to prove that class file matches java file

I recently accidentally submitted my .class file instead of the .java file for an assignment.
After feedback from the grader, I submitted my .java file. Everything worked out fine from there, the grader trusted that it was an honest mistake.
However, is there a simple way for the grader to check to see if the two files really match up?
So far, I've only thought of two solutions:
Compile the .java, and see if the .class outputted is identical/very similar. This is probably very compiler dependant. If the compilers were the same, are there other variables that would make the .class different?
Decompile the .class file, and do a character comparison. This seems like a lot more work, and probably match the .java file even less than solution 1.
Is there a reliable way to check this?
If you compile with the exact same compiler in the exact same enviornment, it is highly likely that you will get identical class files.
However, if there are variations in compiler or platform, you should look at this discussion.
Outside of that, you will probably have to evaluate it functionally. That is, write a test class that exercises all desired behaviors of each class and check whether they all return identical results.
I did a very simple test and it worked for me..
I compiled a simple java program and created its .class file
Then I just change one letter inside System.out.println and again crated a .class file
Then used diff command in linux and it tells me that two binary files are different
So I think instead of decompiling the .class file and then checking both .java files you can directly check for two .class files
At least it worked for me.
Hope this helps!

In the javac command how to give custom class file name?

I am compiling a java source file named MyPeogram.java. Now I want to use javac to compile the file and save the class file in the name MyCompileCode.class. Is it possible to give such custom class names to compiled files?
Please let me know how to do this. I searched online a lot but could not find anything.
No. The name of the source file must match the name of the compiled file.
AFAIK, I think it is not possible, because Java is compiled and interpreted both. Even though you compiled the program and changed, it's name, the interpreter gets confused in class file name and the data in that class file.
Correct me, If I am wrong.

Reconstructing an Executable jar (with Modified Class-Path) from Class Files

I'm grading an assignment in Java. Students are asked to implement a Five-In-A-Row (like Tic-Tac-Toe, or two-player Pente) interface which is used by a GUI .java file. These files (interface and GUI) are given to the students in a file called lab2.jar (where they're in cs251/lab2/ under the names GomokuModel and GomokuGUI, respectively), which the students must add to their classpaths. When the project is finished, students are requested to turn in a .java file called Gomoku.java.
One student turned in a .jar, but the command
java -jar Gomoku.jar
responds with
no main manifest attribute, in Gomoku.jar
I figure the student may have forgotten / not known to make a manifest file. I unzip the student's jar and find only .class files. I try to make my own jar from these files:
According to specs, the main must be in Gomoku.java, whose class is Gomoku.class. So I make a manifest.txt file that looks like
Main-Class: Gomoku
Class-Path: lab2.jar
And try to make a .jar out of it using the command
jar cfm myJar.jar manifest.txt *.class lab2.jar
But when I run this using the command
java -jar myJar.jar
I get the following error:
0Exception in thread "main" java.lang.IllegalAccessError: tried to access method cs251.lab2.GomokuGUI.<init>(Lcs251/lab2/GomokuModel;)V from class Gomoku
at Gomoku.main(Gomoku.java:47)
This particular error is giving me trouble. I've never seen anything like it, and my research on the web doesn't turn up anything. Because the error says it's coming from GomokuGUI, which is one of the lab2.jar files, I think the error's on my end. My questions are:
Can I make an executable .jar when I know and have
What goes in the classpath
Where the main should be
A set of relevant class files
If the answer to (1) is yes: Am I going about it in the right way? I have a feeling I'm missing a recompile step somewhere.
In this particular case, I may ask the student to resubmit. And I will download the .jar's I see submitted before due date to make sure they are runnable. But for knowledge's sake (I myself have made .jar files that have had only .class in them and no manifest), is there a way to salvage a working file like the one described above?
From the JRE javadoc:
public class IllegalAccessError
extends IncompatibleClassChangeError
Thrown if an application attempts to access or modify a field, or to
call a method that it does not have access to.
You're getting
0Exception in thread "main" java.lang.IllegalAccessError: tried to access method
cs251.lab2.GomokuGUI.<init>(Lcs251/lab2/GomokuModel;)V from class Gomoku
at Gomoku.main(Gomoku.java:47)
The method it's complaining about is named <init>. That's what Java calls constructors, internally. It's saying that Gomoku.main() tried to issue new GomokuGUI(model) where model is expected to be an instance of GomokuModel, but that this constructor was not accessible. The fact that Gomoku.main() is in a different package from GomokuGUI means the constructor would have to be public for that to work.
You can check that via reflection -- I believe Eclipse can do that for you, actually -- but that's almost certainly what's going on.
So either the student turned in broken code, or you broke it during your attempts to force-fit it into convenience-executable jarfile format. Which was wasted effort in any case, since you can't grade the assignment based on object code and you're going to have to go back and ask for source code anyway.
If you really want to try running the jarfile the student submitted: Go back to the original unmodified jarfile and try just running 'java Gomoku -classpath myJar.jar' where myJar.jar is what the student turned in. If that doesn't work, try 'java Lcs251.lab2.Gomoku -classpath myJar.jar', which is probably the package they intended to put it into given the error message you're getting. If neither of those runs, ask the student what command line they've been using to run it and try that. If THAT doesn't work, then it's time to investigate why.
The whole executable-jar question is a red herring and a waste of time until you know the code actually runs and what the entry point actually is.

How does Scala handle Java style package statements

This sounds embarrassing. My purpose is to understand how Scala treats package statements, written in the Java style. To this end, I wrote up a little example class (that I named DinnerTimeP.scala as below:
package dinnertime
class Dinner {
val veggie = "broccoli"
def announceDinner(veggie: String) {
println("Dinner happens to be tasteless " + veggie + " soup")
}
}
I have a folder called scaladev, under which I have created the package folder, dinnertime. Under this package lives DinnerTimeP.scala. On the DOS command I then navigate to dinnertime and compile the file DinnerTimeP (the name sounds silly) with scalac as below.
C:\scala-2.9.1.final\scala-2.9.1.final\scaladev\dinnertime>set CLASSPATH=.;C:\scala- 2.9.1.final\scala-2.9.1.final\scaladev
C:\scala-2.9.1.final\scala-2.9.1.final\scaladev\dinnertime>scalac DinnerTimeP.scala
I was hoping to find Dinner.class generated right under the dinnertime folder and sitting next to the source file DinnerTimeP.scala.
To confirm my understanding, I created a HelloWorld.java program under the same folder:
package dinnertime;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I compiled HelloWorld.java on the command line as follows:
C:\scala-2.9.1.final\scala-2.9.1.final\scaladev\dinnertime>javac HelloWorld.java
The HelloWorld.class file was generated right next to its source file.
This was the exact situation I wanted to see with the Scala source file and its compiled file.
Instead I see a new package folder generated by Scala inside the package folder dinnertime.
This might be naive. I am probably betraying a fundamental understanding of Scala and packages, but I was perplexed by this behaviour.
This is the problem I can't explain to myself: Why is the nested package created for the newly generated class file. This is the problem to which I which I had hoped to solve, based on my own sincere efforts
Because my experience with Scala at the present time is limited, I have resorted to asking the Scala gurus on stackoverflow to help me understand what is going on and why?
Is there a reason for this nested package to be created by Scala and not by Java?
Tomasz explained everything you need to get it working, so let me explain the why.
Scala does not mandate that source files be in a directory hierarchy that reflects the packages. That is, Dinner.scala could be anywhere: it simply doesn't matter.
And, to be really clear, even if you have a complex package hierarchy, deep and with multiple subpackages at each level, you can put all source files in a single directory. The directory where Scala files are put in is not relevant.
Sorry for giving it so much emphasis, but coming from Java it might be difficult to grasp this.
Ok, now, how to explain dinnertime/Dinner.class? Well, JVM demands that class file be put in a directory hierarchy that corresponds to the package names, so even if Scala source files can be put in arbitrary directories, scalac must produce an output whose directory structure reflects the package names.
So, to review everything, Scala doesn't care what directory you were in, so it ignored the fact that you were in a directory called dinnertime. However, since the source code indicated that the class was in a package named dinnertime, it created such directory and put the class file in it. It assumed the base for that was the current directory, which can be changed with the -d parameter, as per Tomasz answer.
First of all try compiling from the root directory, so that dinnertime is a subdirectory:
$ javac dinnertime/HelloWorld.java
and:
$ scalac dinnertime/Dinner.scala
They both produce the same output, i.e. in both cases .class file is placed under dinnertime subdirectory.
The difference arises when you run a compiler inside a package. Turns out javac is clever enough to put target binary files relative to root directory, not current direcotyr. This is not the case with scalac which always uses current directory as base. You can easily fix this by using -d parameter:
$ cd dinnertime
$ scalac -d .. Dinner.scala

Running a .class java file from cmd. Issue

class file from cmd with the following command and i get the output:
The error is: "Error: Could not find or load main class CustomerManager.class"
Any idea how i could resolve this issue ?
java CustomerManager
Without the .class extension.
just type in cmd java CustomerManager , no need to add .class
In java command . character is shows a / character and use for path of target class.
For solve your problem, execute your command without .class and extension as following:
java CustomerManager
(You can found by a little searching in web, very pages that explain it.)
Inside your code , keep your class as Public and then execute
java CustomerManager
Also cross check in your IDE if there aren't any spaces before or after the class .
Hope this helps .

Categories