How to change some of the code of a Java program? [duplicate] - java

This question already has answers here:
javac : Compiling a .java file which uses other classes in it
(2 answers)
How to compile a single Java file
(2 answers)
Closed 5 years ago.
Note: I'm only a student and don't have too much experience with programming, so please be patient with me.
I'm trying to modify the code of an existing program written in Java, so that the program runs exactly as normal except for my couple changes.
I found and unzipped the appropriate jar file and put the .class I want through a decompiler. But, after modifying the code, I can't re-compile it. The javac compiler just gives me a bunch of errors, mostly because it doesn't recognize the references to variables and methods that are defined in different classes of the program.
How can I change a couple lines of code in a Java program? Am I on the right track with my current approach (decompile→edit→recompile)?
SOLUTION: I had to include the classpath of the jar. javac -cp "[PATH TO JAR]:lib/*" [PATH TO JAVA FILE]

Related

how to convert java file into executable file with icon [duplicate]

This question already has answers here:
Self-Contained Applications, built in Java
(3 answers)
Closed 2 years ago.
I have a java code that makes input and saves data as a text file. To problem is that I want the user to double-click an icon that contain an .exe file with the java code inside. In order words, the double click on that item replaces the run command on java. I want to wrap the java code somehow. Thank you!
A workaround approach is to pack the java as jar.
It will execute on double click applied java jdk installed and environment variables set.
But still in this approach we don't create a exe file.
Rather run the java jar.
Should work with both windows and ubuntu...
https://askubuntu.com/questions/192914/how-run-a-jar-file-with-a-double-click

why folder structure is not created even though I had taken package statement? [duplicate]

This question already has answers here:
What happens if you compile an empty java file?
(9 answers)
Closed 5 years ago.
I was trying a package concept in java.
I created a Test.java file that only contains a single statement:
package pack1;
When I compile the file using the command as:
javac -d . Test.java
it compiles fine but doesn't create the .class file, nor a folder named pack1.
I don't know why. Can anyone please suggest?
If I change the code to:
package pack1;
class Test{}
and compile the program by using the command
javac -d . Test.java
Then it compiles fine and folder structure is also created correctly.
Please suggest me why it happens that way?
In your original code, you haven't declared any classes - so there wouldn't be any class files. If there are no class files, presumably the compiler sees no need to create the directory structure that would have been necessary for any class files it would have created.
In practice, the only time I've ever seen a Java source file with no classes in is for package information - and that's pretty pointless if there are no classes in the package, too, although it's possible that if you run javadoc it will create an entry for the empty package. As noted in comments, package annotations will end up creating a class file, but they have to be in package-info.java. For example:
// In package-info.java
#Deprecated
package foo;
Compiling that will create package-info.class, creating a directory for it if necessary.

I use notepad++ to look javac's soure code,but when I open ,I just see something I can't understand [duplicate]

This question already has answers here:
Why is a .class file not human readable? [closed]
(5 answers)
Closed 7 years ago.
I open this file with ANSIC code,so how can I solve this problem?
That is not Java source code.
.class files are compiled Java binary class files.
.java files have the source code.
If you are looking for the source code of the Java compiler (javac), you can get that from OpenJDK. But it's a big and complex project.

write .exe file using Java [duplicate]

This question already has answers here:
Compiling a java program into an executable [duplicate]
(7 answers)
Closed 7 years ago.
In C/C++, the .exe file is automatically generated by the compiler when we run the code.
My question is how do you generate the .exe file in Java instead of just hitting run every time when we open up the .java file.
To do that, you'd need to create an Executable .jar file. Here's the instructions on how to do it.

Is there a way to decompile the java class [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Where can I find a Java decompiler?
How do I “decompile” Java class files?
Any tool can decompile the class back to java source? Like the reflector in C#?
See How do I "decompile" Java class files?
About JAD: http://viralpatel.net/blogs/2009/01/decompile-class-file-java-decompiler-class-java-class.html
you can use javap command for that
javap classname
to get the full code i guess there are a lot of java decompilers available on net for free..
A Java class can be decompiled by using a decompiler which takes a .class files as input and gives its Java source code as output.
Refer these questions on stackoverflow for more details.
How do I “decompile” Java class files? (Using external tools)
How to Decompile Java (using javap command that is available with JDK)
Also See
What is the best software to decompile a java class file ?
Where can I find a Java decompiler?
Decompile compiled Java file with proprietary headers

Categories