There is a jar file already deployed and successfully running. And now we need to add a very small string in a class.
These are the steps so far i made:
1.) I already decompile/extract the jar into a folder.
2.) Open a class file in JDgui(java file viewer).
3.) Copy the source code in notepad++ and save as fileName.java
4.) Compile that .java as .class via cmd.
5.) Compile the folder again as .jar file (the same as the old .jar which is running)
Now when i restarted Tomcat, run the system, then it showing the error:
ClassNotFoundException
When i open that jar file in JDGui the class is in it.
Please help. Thanks
This is no doubt coming from the error you naming the class differently or placing it in a different package (different folder).
When you save source of the class in a Java file, make sure you save it in a folder that reflects its package, and also place the package statement at the beginning of the file, reflecting the folder structure properly.
Also note that if the class is declared as public, it must be in a file named after the class: ClassName.java else it won't even compile. So you saving it in filename.java will not work in this case.
Also make sure you get no comile errors when you recompile this java file after modifying it (else you will not get a result .class file).
Related
it's my first time working with a .class file in java, I saw that a.class file is just a compiled java code, so I figured out I should add this class to my package and it should work like a regular java class file but it didn't, I'm working with Netbeans by the way.
the problem is that I cant use this class, the main class doesn't recognize it, asking me if I want to create a Rational class to fix the error
I am not entirely sure what you want to do with the class file, but my assumption is that you got it already in this form and want to use some of it's methods.
So if this is correct you should put the file in the folder that netbeans uses to store .class files
(in my case build/classes/java/main/Testing):
IDEs almost always store .class files in a separate directory so that you don't have to deal with them, because a .class file is compiled java code and in 99% of the cases you won't touch the .class file but you will change the .java file and compile it to a class file.
Also be careful if your .class file is a part of a package you should replicate the structure of the package like answered here.
I did some robot framework python examples with pybot, and referenced .py files as my library files. The folder structure I used was test/lib/myLib.py and test/test/myTest.robot, where /test was at the same level as the /src folder in my project in eclipse.
I then tried to do similar things with a java project using jython. I have /test/test/myTest.robot which imports the library ../lib/myLib.java. I even tried importing this file using RIDE, but it always shows up as red text, meaning the import failed. The specific message I get from using jybot on the command line is:
"Importing test library 'C:\Users\cody\git\myProject\test\lib\myLib.java' failed: ImportError: No module named myLib"
I read that I might need to add it to classpath, and I think in order to do so, I need to make it a .jar file. I'd rather not do all that if possible to just leave it as a .java file. I attempted to add the lib folder to the build path... By that I mean I added the /test/lib folder to the "Source folders on build path". I also exported the darn thing as a jar and added that as a library. All this was done from the project properties in Eclipse.
I'm pretty stuck... any ideas how to get this working?
Java being a compiled language, you need to compile your java Class before importing it with RobotFramework.
Normally, Eclipse would do that for you, and put the resulting .class files inside a bin repository. If not, you can issue the javac path/to/file.java command, and move the resulting .class file where you want it (somewhere referenced by the classpath.
From within the .robot file, you should have the line Library test/lib/myLib, without neither .java nor .class at the end.
I have decompiled a jar file,
and made two classes from it. After that, I tried to make a new jar file with these two class files, using this code
jar cvf AB.jar WinRegistry.class StartPageChangeApplet.class
The file created without any errors. However, when I look at the source code on Java Decompiler, it says "Internel Error", means that I couldn't make the jar file properly.
Where am I doing doing wrong ?
Please define "made two classes from it". Which java compiler (e.g. javac.exe) are you using? Did you just copy the source to a .class file without compiling maybe?
The java decompiler JAD actually displays source code, not class bytecode. Don't get confused by the title of the editor which is saying WinRegistry.class.
So you can't just save that as a .class. You need to save it as a .java and then compile it to .class using a java compiler:
javac WinRegistry.java StartPageChangeApplet.java
jar cf AB.jar WinRegistry.class StartPageChangeApplet.class
From Eclipse, you can do this way..
I started off with a .jar file that I unzipped. I need to change a few lines of code in just ONE of the classes contained in the contents of that jar file. I went about this as follows:
1) opened the class in Java Decompiler to view the source.
2) copied source to a new text file and saved with ".java" extension.
3) in command line I went to jdk folder and executed javac Classname.java to recompile.
However this class code imports some dependencies so the recompile failed. I have the dependencies, they were part of the original jar file contents but they are all compiled .class files and spread across several folders...
Is it possible to re-compile this class successfully? Is there command line code to include dependencies?
Yes, use the -classpath option.
javac -classpath original.jar Modified.java
Then, you can remove the old class from the jar file and insert the new one. There isn't a simple way to do this via command line, so I recommend an archive application such as WinRAR or 7-zip.
I have a client/server program that attempts to send and receive an object.
There are three packages: server, client and shared
shared contains only the Message class
I put Message.java from shared package into the same folder as calcclient package source files and calcserver package source files.
I compile using the line: javac -classpath .; (long list of client or server.java files) Message.java
They can compile.
Then I change directory up one level and ran with: java -classpath .; .Main
When I use Netbeans to run, the entire program works as per normal. But not if I run from command line. If its executed through command line, the program will work until it needs to use the Message object. Then it will show a NoClassDefFoundError
Am I putting the right files at the right places? How do I get the program to find shared package through command line?
The files are not in the right place. The Message class belongs to a different package so it shouldn't be living with the other classes. From http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html :
User classes are classes which build
on the Java platform. To find user
classes, the launcher refers to the
user class path -- a list of
directories, JAR archives, and ZIP
archives which contain class files.
A class file has a subpath name that
reflects the class's fully-qualified
name. For example, if the class
com.mypackage.MyClass is stored under
/myclasses, then /myclasses must be in
the user class path and the full path
to the class file must be
/myclasses/com/mypackage/MyClass.class.
If the class is stored in an archive
named myclasses.jar, then
myclasses.jar must be in the user
class path, and the class file must be
stored in the archive as
com/mypackage/MyClass.class.
You have a couple of options:
The best solution is to take the time to learn Ant. Netbeans projects are built with Ant, which is a really great feature of Netbeans in my book, and you can open up the build.xml in your project and find a reasonably well commented description of what Netbeans does to build your project. And really I don't think there would be many places around that run builds from the command line so learning something like Ant would be a great help.
The next level down in sophistication would be to manually build a Jar for your shared package and put it somewhere on the classpath.
The most basic approach is just to compile the java files into class files and put them in the appropriate directory reflecting the package name as explained in the quote above.
If you build your project in NetBeans, you'll see that there is a dist folder where you can find your project in binary code. After building the source code, NetBeans specifies how should you start your project from command line.
If you use this and the problem persists, you should rebuild your Message class as a library, link it to the project using NetBeans and the project should work from command line using the command specified in NetBeans.
If you want to manually compile your source files, I think the best solution is to google something like this:
manually compile Java source code
Quote the classpath value if it contains spaces.
java -classpath C:\Program Files\java\...;C:\...
^
This is what's killing you slowly.
Try it this way:
java -classpath "C:\Program Files\java\...;C:\..."