How are commands and their functions defined in Java? [closed] - java

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 6 years ago.
Improve this question
Does Java use libraries to determine which commands perform specific actions?
After learning Java for a few weeks now I don't really understand how Java recognises which commands perform specific functions in code.
For example, how does Java know what to do when you use a "do while" loop? And how does Java remember so many different commands, is there some sort of master list or a combination of libraries that document recognised Java commands and their functions?
This may seem like a real noob question, but it's been bugging me for a while and is getting in the way of my understanding of how code, and specifically Java works. Thank you all in advance.
Edit: Just to make this more specific, I was confused because I didn't know how new functionality is added. So, as I understand, new commands and functionality is added to Java using additional packages alongside the JDK, which contains a list of the most fundamental Java commands?

The commands and the full syntax of the language is defined in the Java Language Specification. The Java compiler generates low level instructions (i.e. bytecode) according to that syntax for language constructs like the do-while loop. The JVM is then responsible for executing those bytecodes.

I understood your questions in two ways:
1 - Where does java hold its lots of functions, classes, etc.
2 - How does java work (how does an if/else work) under the code's skin.
First, there is no noob questions, we are all here to learn :)
Second, about the questions:
1 - Java holds its classes in the Java Development Kit (JDK). Because of this, when you create a Java application, you need it installed in your machine, so Java will find a lot of its classes in the installed JDK.
You can expand the amount of classes by adding new packages (jars) to your project. Then, Java will see all the JDK classes and your added jars.
2 - If you mean to "how does java work in a do-while loop or something related, you want to learn about javac, java's compiler.
Javac will get your code and transform into an jar file. The Java Virtual Machine interprets the compiled code to do memory operations.
For exanple, when you assign a variable to java, internally you create a space in memory with the datatype's size.
When you do a while loop, the JVM will use other VM functions to do the job.
That's actually a quite advanced question. I think you can find more about what you want here.
Hope it helps! :)

Related

scala vs java for Spark? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Can someone help me understand why people is using scala over Java for spark? I have been researching but haven't been able to find a solid answer, I know both works fine as they both run on JVM and I know scala us functional and OOP language.
Thanks
Spark was written in Scala. Spark also came out before Java 8 was available which made functional programming more cumbersome. Also, Scala is closer to Python while still running in a JVM. Data Scientists were the original target users for Spark. Data Scientists would traditionally have more of a background in Python, so Scala make more sense for them to use then go straight to Java
Here is direct quote from one of the guys who wrote initially wrote spark from a reddit AMA they did. The question was:
Q:
How important was it to create Spark in Scala? Would it have been feasible / realistic to write it in Java or was Scala fundamental to Spark?
A from Matei Zahara:
At the time we started, I really wanted a PL that supports a language-integrated interface (where people write functions inline, etc), because I thought that was the way people would want to program these applications after seeing research systems that had it (specifically Microsoft's DryadLINQ). However, I also wanted to be on the JVM in order to easily interact with the Hadoop filesystem and data formats for that. Scala was the only somewhat popular JVM language then that offered this kind of functional syntax and was also statically typed (letting us have some control over performance), so we chose that. Today there might be an argument to make the first version of the API in Java with Java 8, but we also benefitted from other aspects of Scala in Spark, like type inference, pattern matching, actor libraries, etc.
Edit
Heres the link incase folks were interested in more on what Matei had to say:
https://www.reddit.com/r/IAmA/comments/31bkue/im_matei_zaharia_creator_of_spark_and_cto_at/

Can batch programming use to read variables that inside .java and make change of it? [closed]

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
Can I use batch programming to read java codes and identify the variables (inside .java or .class) and create a loop at batch programming and modify the variables?
For example
**Inside java**
String s = "abc";
Can batch programming read into java and retrieve the "String s" and modify using batch programming?
Thank you for your views, comments and answers.
Let me explain why am I asking this question.
I have a java code that able to pull files from FTP. There are many folders inside the FTP. And the java code able to pull files from each folder.One year got 52 weeks means that the FTP got 52 folders. I just want to pull files up to 3 weeks back but not 52 weeks. So I suggested them to create a simple function into java codes to pull files up to 3 weeks. But they said they prefer not to touch the java codes. Because they said in future there might be other vendor use this java codes to pull files.So they think that if create a batch file to solve this problem, in future they just need to change the setting in the batch files. They said "if I make changes in java,in future if there are 10 vendors, are you going to maintain 10 java codes?" So they request me to use batch programming to do it. =/. So I am asking is it possible to use batch programming to read variables and modify the variables and return to the java codes. If still unclear, please do ask me. I am willing to answer it.
My question is can I use batch programming to read java codes and identify the variables (inside .java or .class) and create a loop at batch programming and modify the variables?
In theory, yes.
In practice, it would be tricky (i.e. you would need to do some difficult Java programming to achieve it). And it is a BAD IDEA to modify the code like that, either at the source code or compiled code level. There are much simpler, safer and more transparent ways to implement the setting of configuration variables.
The normal way to handle variables that need to be changed for different runs of a program is to load the variables from a configuration file, or set them based on the command's command-line arguments, system properties or environment variables.
I will take note of this and let them know what situation I am in right now. Hope they will understand.
It is your job (as an IT professional) to convince them that they shouldn't ask for something really stupid ...

How to inject bytecode to a compiled java program without using tools? [closed]

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 want to learn how I can create an injectable piece of java bytecode, and inject it into an already compiled java program so it will run when the said program is executed.
It doesn't have to be dynamic and in runtime, just given a compiled java program inject additional code into it.
Now, I know there are many existing tools for this, like Javassist and ASM. But the act itself isn't my goal, I want to learn how its done, so I want to learn how to do this without these tools.
For example: How to strip excess code from the source bytecode, where to inject it into the target code, etc.
The best answer would be one or more simple pieces of source or pseudo-code.
After learning and successfully doing this I'm going to start searching info on how to do this to Linux executable binaries, so adding in more information on that way would also be very helpful and appreciated.
First off, Java classfiles are essentially immutable once loaded, so what you're really asking is how to create and modify classfiles by hand.
The answer is to read the JVM specification. That's how I got started with bytecode. After reading the specs, I wrote a couple simple classfiles by hand in a hex editor and played around with it to see how things worked. Of course, that's not practical for normal usage, so I later wrote an assembler. It's not that hard.
Incidentally, the source code for my assembler is only around 1k lines of code, so it's a lot less to sort through than Javaassist.
Javassist is essentially decompiling and compiling the code, that is why there is a lot of code there.
And you won't find the type code injection you are looking for in javassist. So "Go read javassist" is a rather stupid suggestion.
If you want to put your code into an specific place(for instance in the start or constructor)
you can see how to find the spot by reading JVM docs.
However as Antimony mentioned, you are looking for bytecode knowledge, so here it is:
http://arhipov.blogspot.com.au/2011/01/java-bytecode-fundamentals.html
If you want to inject a piece of bytecode,you can just find the start of your main() and paste the code there. It will be 200-300 LOC MAX.
With linux binaries it is much easier, read this:
http://www.skyfree.org/linux/references/ELF_Format.pdf
Typically, this is done by reading the bytecode (or Linux executable), transforming it into some form of Intermediate Representation (IR), perform additional transformation on the IR, and convert it back into the original format.
If you transform the IR back into Java source code, you will get a decompiler for Java. If you perform analysis on the IR, manipulate it (addition, removal, rewriting, etc.) and convert the transformed IR back to the bytecode format, you will get what you described.
For detailed algorithms on how to convert bytecode to an IR, you can refer to Section 3.3 of http://suif.stanford.edu/~jwhaley/papers/mastersthesis.pdf and Section 3 of https://courses.cs.washington.edu/courses/cse501/01wi/project/sable-thesis.pdf

Making an executable with most Java code, but maybe another language added in [closed]

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 9 years ago.
Improve this question
I have been looking everywhere, and being a novice at Java and lacking proper terminology it seems impossible to find this.
I assume the correct term is "wrapping", but looking at other Java applications, such as: Minecraft, and seeing how pretty the launcher is and custom buttons, it seems impossible to accomplish such a feat with JFrame.
Do they code their own sort of frame? I've heard that you can "wrap" Java code in like C++ to make the launcher, and after passing the screen is launches the game (minecraft.jar).
Also, how is the exe file created? That once it is run it checks for a patch from the server, that installs the necessary files in a given directory and checks there for save files?
Is it another language? if so which? Which do You recommend? A resource for learning all of this?
These are the main aspects that I've been struggling to learn as no one seems to ask a similar question as I.
Also, considering Java is cross-platform, my main targets are Windows > Mac, for now I can't be bothered to worry about Linux considering I'm already struggling so much here; so for now, how to create a Exe file, if need be, in C++ or any other language, then perhaps DMG for Mac.
You have a lot of things to consider. I would suggest that for now you use Java WebStart to launch your application. This gives you things like:
Easy distribution - put files on a web server somewhere and a JNLP file describing how to glue your files together.
No messing with EXE-wrappers and multiple distributions - this is in the standard Java installation.
Easy updates - just create a new set of files (don't update jars in place) and a new JNLP and thats it.
A sandbox without any effort. Full access to the machine needs code signing which these days is a moving target. Sandbox should be enough for quite a bit though.
Then when you find you have enough users to warrant platform-specific distributions, you can add these as needed instead of getting everything working up front, where you should be concentrating on your application instead.
If you do not want to go the JNLP route, there a number of tools which can do what you ask for.. my favorite is launch4j, which is easy to use via the launch4j-maven-plugin, see e.g. this launch4j-demo illustrating how to use it.
PS: Alternatives include http://sourceforge.net/projects/jsmooth/ or http://www.ej-technologies.com/products/exe4j/overview.html (.com). Note that similar Qs to this with more have been asked on How can I convert my Java program to an .exe file?, How Can I convert Java To Exe, Best free tool to build an exe from Java code?

How to create custom JVM? [closed]

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 9 years ago.
Improve this question
I was reading item #6.10 on http://www.cafeaulait.org/javafaq.html and I began wondering how the big players go about creating their own implementation of a JVM. Would an experimental something or another be possible (and feasible) for one guy?
technically, all the information people need to create a new JVM is in the public specifications for the language and the targetted platform. A JVM would need to behave differently depending on whether it is meant to run on a desktop computer or a mobile phone, even if the bytecode interpretation would be largely identical.
A few places to start looking for information:
http://en.wikipedia.org/wiki/List_of_Java_virtual_machines
Reading The "Java Virtual Machine Specification" by Tim Lindholm
http://www.jcp.org/en/jsr/detail?id=30
From what I have seen of JVM implementations by Sun, IBM or smaller companies like Esmertec, writing a simple JVM is a several man-months project but adding JSR after JSR to support more functionality can take years afterwards.
Now, if all you need is a simple bytecode interpreter, it's not that bad, but it's still quite a bit of code to write.
A handmade JVM would be a great way to learn about virtual machines in general, the issues of program language design (through the JVM spec), and the nitty gritty of parsing and so forth.
If you choose to take it in that direction, you could also explore optimizations, which is where it can get interesting, and you can take research papers and implement their algorithms.
That being said, if you're less interested in the long and arduous task of creating a VM from scratch, you might want to modify an existing open source VM like Kaffe. It will show you what a virtual machine does, but not necessarily how Java code works in Sun's JVM:
Kaffe is a clean room implementation of the Java virtual machine, plus the associated class libraries needed to provide a Java runtime environment.
This way, you could study the details, but dive in to implementing more interesting features.
I understand that, currently, the big players license the Java library from Sun. They then add their own refinements. The main difference between implementations is the bytecode->machine code compiler.
For one thing, you may want to have a look at Apache Harmony They have come a long way, so their project history may actually give you a good idea on the effort required. I myself would not bet on it being feasible for one guy

Categories

Resources