I have some .class files that I need to convert to .java so I did:
javap -c ClassName.class
and all the time I have the same error
ERROR:Could not find ClassName.class
Do you guys have any idea of what might be the cause? I did man javap and as far as I know, the syntax is correct. If there is another way to convert it to a .java file, I am more than willing to try.
Invoking javap to read the bytecode
The javap command takes class-names without the .class extension. Try
javap -c ClassName
Converting .class files back to .java files
javap will however not give you the implementations of the methods in java-syntax. It will at most give it to you in JVM bytecode format.
To actually decompile (i.e., do the reverse of javac) you will have to use proper decompiler. See for instance the following related question:
How do I "decompile" Java class files?
I'm guessing that either the class name is wrong - be sure to use the fully-resolved class name, with all packages - or it's not in the CLASSPATH so javap can't find it.
I used the http://www.javadecompilers.com but in some classes it gives you the message "could not load this classes..."
INSTEAD download Android Studio, navigate to the folder containing the java class file and double click it. The code will show in the right pane and I guess you can copy it an save it as a java file from there
Step 1: If your class file is inside a jar, rename the .jar extension to .zip and extract the zip folder
Step 2: Using the below online decompiler, upload your .class file and read the contents
http://www.javadecompilers.com/
This is for Mac users:
first of all you have to clarify where the class file is... so for example, in 'Terminal' (A Mac Application) you would type:
cd
then wherever you file is e.g:
cd /Users/CollarBlast/Desktop/JavaFiles/
then you would hit enter. After that you would do the command.
e.g:
cd /Users/CollarBlast/Desktop/JavaFiles/ (then i would press enter...)
Then i would type the command:
javap -c JavaTestClassFile.class (then i would press enter again...)
and hopefully it should work!
Related
I have a pile of .java files. They all have the same class name public MyClass. They all have a main method. They all may or may not have a package declaration at top, and I do not know ahead of time.
I am trying to write a script to compile and run these java programs. This is easy for the files without the package declaration... I just do some cp operations to setup, javac MyClass.java and java MyClass, then rm to teardown. However, the files with the package declaration require special attention. I have a few options that occur to me, including deleting the package lines, or attempting to read the package lines so that I know what the resulting directory structure should be. Both of these require me to go parsing through the .java files, which makes me sad.
Is there a way to compile and run these files without having to parse the .java files? Something like:
javac --ignore_package_structure MyClass.java
would be ideal, but a quick look at the javac man pages suggests that such a thing doesn't exist.
If we can assume that each student submits a single source file named HelloWorld.java, then we can use the "Launch Single-File Source-Code Programs" feature added by JEP 330 in Java 11:
java HelloWorld.java
We don't run javac first, we don't get a .class file (no cleanup needed), and any package declaration is handled automatically.
Remember, the students are still allowed to use many classes, they just all have to be submitted to you in a single source file.
The name of the class doesn't even matter. The first class in the source file is executed.
There isn't any easy way to do this. You could use regex though, and replace all imports with this simple java regex:
"package \w+;"g
Simply stated, you create a Java program to replace all the package names.
How to replace files: Find and replace words/lines in a file
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
I am writing some code to my phd project and I am using VIM as my code editor.
As I am coding in Java, I chose Syntastic to check and compile my code. So far so good.
My issue comes when I try to create a directory with all my .classes. I want to do this, because then I intend to create a .jar using this directory using a simple make file. So, this is my scenario:
source code:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\src (all .java)
class files:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes (where I want to put all the .classes)
In this way, let's say I am coding br.ufrn.Project. When I use :SyntasticCheck, I want the br.ufrn.Project .class file to be generate at:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes\br\ufrn\Project.class
and not at:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\src\br\ufrn\Project.class (the same of the .java)
Here goes the options that I am using at my _vimrc file
let g:syntastic_java_javac_classpath = 'C:\Users\LABIMD05\workspace\szz_lib\*;C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes'
let g:syntastic_java_javac_delete_output = 0
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_mode_map = { 'mode': 'passive',
\ 'passive_filetypes': ['java']}
THE PROBLEM:
Everytime I compile br.ufrn.Project file, the .class file goes to the same directory of the .java file
I thought it would be because Syntastic would create the .class file in the current working directory. Then I used:
cd C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes
To see if Syntastic would create the .class in the desired place. But I had no success.
Would you guys have some clue where can I configure it? I just want to separate the .class files from .java files and then use a make file to create a jar with the binaries only. Simple thing.
Thank you for any help you can provide.
You can't configure Syntastic to compile the java files to a different location. However, you can make a command that uses SyntasticCheck and compiles the java files to a different directory.
Using the javac -d dir File.java command you can tell the java compiler where to generate .class files.
Using this you can make a vim command, I called it Javac but you can choose what to call it. It will call Syntastic check and generate the .class files to the other file.
function! Javac()
execute "w"
execute "SyntasticCheck"
execute "!javac -d C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes %"
endfunction
command! Javac :call Javac()
If the SyntasticCheck part is not necessary you can remove that.
Just put this in your .vimrc and then you can use :Javac to execute it.
Alternatively you could also put it in ~/.vim/ftplugin/java.vim if you want it to only be active when editing the java filetype.
I had deleted a complete folder by mistake and had to use a data recovery software. However I could find only the .class files of my java program. The DE-compilers on net are giving error.
Even when I am trying to run the class file from command line using java..it gives incompatible magic value: 4292411361
1)How can I correct this error and run my program from the class file i just recovered?
2)How can I DE-compile this class file?
thanks
If it is a matter if decomiling the .class file, I would reccomend you user JD GUI
It is free and quite good in .class decompiling.
Then you can rebuild the Class file.
you can find error explanation of incompatible magic value error on https://stackoverflow.com/a/2390763/3131537
java compiler is very good tool for decompile class http://jd.benow.ca/
1)may be Your class file not recovered properly.if recovered properly try the below solution
2)we can use jad compiler to get the source file from .classfile.Download the jad compiler.we get a zip file,unzip it.you will find a .exe file in the folder.
place the .class file in the same folder.
use the command
jad -sjava Filename.class in cmd.
I hope this question is not repeated. But just can't find answer anywhere:
I have ONE folder containing two files one A.java another B.class.
Now in A.java I am trying to declare
public class A extends Applet{
...
B aB;
}
The compiler gives me:
B cannot be resolved to a type
I read a lot of posts that say if the files are in the same folder, I don't need to import. Could anyone help me to "resolve" this problem?
Thanks much appreciated!
-----------SOLVED! - SEE ANSWER BELOW------------------
The .class files need to reside in a directory referenced by the classpath variable. Usually you put your .java files in one directory (src), compile to another directory (bin) and have external .class files in a third directory (lib). The commands will look like this:
# compile
javac -sourcepath src -classpath lib -d bin
# run
java -classpath bin:lib A
Using an IDE like eclipse should help a lot here as it takes care of most of the details
The simple case that you've posted works for me. I'd check the following things:
Are you sure that B.class is present in the same folder as A.java?
Are you running javac from that folder?
Have you typed the class name B correctly everywhere in your program? This includes capitalization, as Java identifiers are case sensitive.
Are there any package declarations in your program? If there are, none of this is going to work, since you're implicitly using the default package by just throwing everything into a folder.
The compiler looks for *.class file in its class path. It will only look for *.java files in the same source directories. You need to set the class path to include the directory.
Or you could use an IDE which sets all this up for you and saves a lot time in the process.