Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to programming, so please bear with me; I will try to make my question as clear as possible. Responses are greatly appreciated as I am struggling quite a bit with the following question...
As you'll likely know,
"Inheritance [in Java] allows the compiled form of classes to be modified and/or extended. If a class is available that almost satisfies your requirements, you can adjust it..." (Dos Reis, 2012)
My question:
How do you compile a .java file containing a subclass who's class that is
Only available as a .class file
Outside the current working directory
I hope this is clear, and once again apologies beforehand for lacking the expertise to phrase this question more clearly.
Pretty simple: to the java compiler, it doesn't matter if a class comes as source or compiled.
The only thing: a compiled class needs to be on the class path of the javac invocation.
Keep in mind: any class inherits from java.lang.Object. You do not have Object in your source path. It only exists as class file in some JAR coming with your JDK!
In other words: when the compiler finds source code, it uses that. If not, it will check the classpath given to it and search for a compiled version of the class you intend to use.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to mutate a function of a class that is loaded at runtime (it has a bug in it but the project is long gone so i cannot build the binary). What i want to do instead is write a piece of code which will run during the application initialization phase and mutate this function so that it works fine. And simply keep that code around until the replacement is ready.
Having no experience with bytecode modification what library could i use to modify and reload a class at runtime? Specifically i need to replace a throw instruction with a noop instruction (i did this once using hex editor but lost the binary).
Also if you know any tutorial on how to do something like that please share.
I can see many libraries for doing this but i cant know which ones are good/bad do the job...
I think use Java Attach API. Java Attach API is procedure of loading a Java agent into an already running JVM. you can understand the work of javaagents by reading the Java Instrument javadoc. AgentMain help to you.
Agentmain is invoked when an agent is started after the application is already running. Agents started with agentmain can be attached programatically using the Sun tools API (for Sun/Oracle JVMs only -- the method for introducing dynamic agents is implementation-dependent).
This tutorial is useful about java instrumentation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to build a command line interpreter that implements "cd" command in linux. I don't know how the function that suppose to do that shoud work.
I know that this question has already been answered here:
Changing the current working directory in Java?
But I'm new to Java and I read alot about this and still don't get how new File(parent, path) would help me change my directory. Any help please?
It won't help you change the directory; it is what is advised to use instead of changing the current working directory. The position of the designers of Java is that, if some part of your application needs a specific context directory other than the current working directory inherited from the underlying OS, this should be done by relying on the explicit two-argument File constructor (and likewise for any other file-based APIs).
Note that this makes a lot of sense because the current working directory is effectively global state, and mutable global state is a well-knows Pandora's box of untraceable bugs.
I used a global variable that holds the path of my current direction. cd would just change it and all other commands use/modify it accordingly. Nothing simpler I guess.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to ask you about sentence: "In Java we run classes, not programs". Is it correct? I know the build process, but it seems to me that this sentence is ambiguous
(As this is so wide-open, let's make the answer a community wiki...)
I've never heard anyone say that (and would argue that it's so vague and slightly misleading you should ignore it).
Without context, it's hard to be too specific about what it's meant to mean, but it probably relates to the fact that many of the ways that Java programs are run involve a specific class that is the starting point.
For instance, if you're running a boring old program from the command line:
java DoSomething
...that's saying to run the main method in the DoSomething class.
Similarly, in an executable jar file, the manifest in the jar says which class to run the main from.
Similarly, a servlet is identified by a specific class implementing the appropriate interface and set up in the Java EE container's configuration.
But again, it's a really odd thing to say, not least because although the entry point may be a single class, of course that class then ends up using others to get its work done.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to compile and emit .class files at run time? I have some generated servlet code and I want to compile them into classes and package it as a war.
Thanks.
Just export the generated codes into files in a temp directory, invoke javac in there, package them, serve them. Nothing fancy needed.
Yes, it is.
You can take a look at the Java Compiler API doc
Note however, that you will have to provide the corresponding ClassLoader and manage all the resources yourself.
If you want to generate bytecode from non-Java sources, you can also use ASM directly:
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
As the title says, with only using Java code, so no bytecode, is it possible to crash most of the general java decompilers, let's say the ones you find by googling "java decompiler"?
I am aware that this will not stop people from building a specific decompiler that gets around the issue, but it will surely keep people away from simply decompiling files.
I was thinking along the lines of the following silly piece of code which I hope to never meet in production:
final public class X {
//... interesting stuff
#Override
public String toString() {
return toString();
}
}
This could be a possible counter measure against people wanting to print out your object, as an example.
It depends on the implementation. It might crash if they don't recognize the latest byte code instructions, and it might crash if they can't figure out what to decompile your byte code instructions to. However in my experience when decompilers cannot figure out what to decompile the byte code to they will simply dump the byte code that they can't figure out within the rest of the Java source code they have already decompiled. A suggestion to attempt to see this is to decompile programs written originally in another JVM language like Scala or Clojure.