Can Java run directly on hardware (assuming there's enough memory to include the necessary JRE/JVM? If it can, how does System.out.println work? I'd think there's nowhere for that output to go if it's all just on a cpu.
Directly on hardware? I am assuming you mean to ask if Java can run on a micro-controller? The answer is yes. The JVM is a virtual machine which is essentially its own operating system. The JVM was designed to do exactly what your wondering about. The JVM's two primary functions are to allow Java programs to run on any device or operating system, "Write once, run anywhere" and to optimize memory solutions.
To answer your second question. In order to visually see the output of a System.out.println() call one would simply need to provide the micro-controller with a screen. However, in theory the code would still execute without you seeing it being displayed. So one could write a Java applet that prints "hello world" and then load it onto a micro-controller and run it but that's just silly.
EDIT: I assumed you were not asking: "Can you program a micro-controller with Java" Silly of me, the answer is yes you certainly can; however, you wouldn't want too because the JVM is rather large and it would take up a lot of space. That being said, if you are interested take a look at: the STM32 Java-ready micro-controllers or the Renesas RX. Also, you could run a gutted JVM using uJ or NonvoVM.
The short answer is no, due to the JVM and write once run anywhere feature in java, the code is not ran directly on the hardware but within the JVM... THE JVM essentially acts as a middle man between different hardware/OS...if you look to accomplish this then take a look at C++
Related
In Java, only few things are considered to be atomic (What operations in Java are considered atomic?). For example i++ consists of 3 different atomic operations: Load i into a register, add 1 to that register, write the new value of the register back to i. Something like that.
My question: Is it possible to "parse" Java Code into a sequence of it's atomic representation? So when input is "i++" i don't want to have i++ as output, i want to have "LOAD I TO REGISTER", "ADD 1 TO THAT REGISTER", "WRITE I BACK FROM REGISTER". Is that possible?
Googling didn't help much on that topic.
In Java you don't have register access or assembly code access. The only way you can realize the above is to use Atomic classes as provide in the java.util.conrrent.atomic package. However, that would be more costly than a simple i++ unless you are doing this on a multi-threading background.
Yes and no. Short answer: it'll be very very hard.
The code will go through several steps before it arrives at the hardware where you could 'parse' the atomic representations.
It will first be compiled into bytecode. This is already closer to machine code, but not yet what you want. You can read it by opening the class file in an editor that can parse bytecode (Eclipse IDE can do this).
When you run the program, the Java Virtual machine will execute your bytecode. It may, depending on the situation, execute the bytecode as a scripting engine, compile it to machine code or change the bytecode and then execute it to get better performance. What the Java Virtual Machine does will depend on the system, the current load on your system and the bytecode. So it's actually impossible to predict the actual machine code it would generate before hand.
For a specific run of code, you can use an assembly debugger (disassembler) such as IDA. It will allow you to view what is being executed on the system as assembly code. You will most likely see a lot of code that is part of the Java Virtual Machine and not of your program. This will make things even more difficult.
I'm a front end developer that's looking to get into some other languages such as Java or C++. I have an idea for a program and was just looking for an answer to something. What I would like to do is build a program and boot directly to that program. For example I have an old computer and I wipe the hard drive clean. So they is nothing currently on it. Not even an OS. I want to build a program that I can install to the hard drive that will boot straight into the program once started. Would this be considered an OS?
No you don't. Unless you want to spend many years, writing drivers for your graphics card, harddisk controller, usb controller, dma controller and all the other hardware your computer have.
What you want is a minimal operation system, which include just the kernel, and a runtime library and which start your program and nothing else on startup. A minimal Linux such as linux from scratch or bsd would be a good starting point.
First of all you need to decide your your program needs what. I mean should operate in Protected mode or the routine you have is tiny, so it is enough to run before entering protected mode (i.e. in real mode).
Here you can do three things
Modify bootloader to jump the execution to your code . Then Your code can resume normal os initialization.
Modify your os kernel early initialization code So that it executes your code before entering protected mode
I think your code will not be harmed if a bit of os portion is running. So you can write your routine before full kernel initialization.
Now note that for the later two point you need to modify your kernel, which is not easy (not even always possible)
Now the problem in first approach: Nothing will be ready for you, not even a regular c library or divice drivers , so you have to write every raw bit of code by hand which is crude.
This is off course not possible in java. Because the jvm will not be ready for you.
Now practically: there are lot of tiny os available, use one of them and modify as per your need. use this link to get a complete list of what is available for you.
First, Java is right out. You cannot possibly do this in Java without enormous amounts of tool-building. Java is not suited for this task at all.
You can do it in C++ or C. The search terms you are looking for is operating system development. This would probably not technically be considered developing an Operating System since it wouldn't run other programs, but the information about how to get through the boot-up procedure and establish a minimal environment are going to be most easily found in the category of operating system development. Some reasonable starting resources for that can be found at the OS Dev Wiki.
Alternately, you could take an existing small open-source OS and modify what it does after the boot-up sequence completes. If your program is intending to do anything more than just use the keyboard and the screen in text mode, there need to be device drivers. Thus, depending on the project, changing an existing OS may be the easiest route because you won't need to write your own device drivers for any devices you want to use.
Java can't run without Environment. If you want to run you program on you machine without OS, Java is a wrong choice.
C++ program can run without OS, but it's difficult to write a bootable program in C++.
If you want to write your own bootable program, you should use assembly for boot and load function, with some knowledge to use hardware in low level.
You have to have an operating system, so your program would be the operating system (or you would have to use another one and write it for that). It's certainly possible in C++, but it's not really possible to write an operating system in java.
Unless you want write something in (for example) Open Firmware and Forth or say a ROM BASIC. You'll probably qualify as a boot loader. Your application may qualify as an operating system. In my opinion, and a modern context, it entirely depends on how much functionality it provides to hosted applications. I'm not sure that something like FreeDOS would be considered an operating system (no pre-emptive task scheduling or GUI for example) given modern computers (I don't care to argue the point either way).
Is there some way to get reasonable (not noticeable) starting times for Java, thus making it suitable for writing command line scripts (not long-lived apps)?
For a demonstation of the issue, take a simple Hello World program in Java and JavaScript (run w/ node.js) on my Macbook Pro:
$ time java T
Hello world!
real 0m0.352s
user 0m0.301s
sys 0m0.053s
$ time node T.js
Hello world!
real 0m0.098s
user 0m0.079s
sys 0m0.013s
There is a noticeable lag with the Java version, not so with Node. This makes command line tools seem unresponsive. (This is especially true if they rely on more than one class, unlike the simple T.java above.
Not likely, only thing you might be able to try is a different implementation of the JVM, but that probably won't change. Most Java apps are (relatively) long lived though and possibly interactive, which means the JVM startup time becomes lost in the noise of normal uses.
Have you actually tried timing a Java command-line app called repeatedly, though? I would expect after the first incarnation for the start-up time to be alleviated somewhat by the library classes being in the file system cache.
That said, yes, the Java platform is not one of the simplest and in any case you're not going to compete with a small native executable.
Edit: as you say that the timings above are for "warmed up" calls, then a possible workaround could be:
write the gubbins of your commands in Java
write a simple local continually running "server" that takes commands and passes them to the relevant Java routines
write a simple native command-line wrapper (or write it in something that's fast to start up) whose sole raison d'ĂȘtre is to pass commands on to the Java server and spit out the result.
This ain't nice, but it could allow you to write the gubbins of your routines in Java (which I assume is essentially what you want) while still keeping the command line model of invocation.
As others have said, the plain answer is just "not really". You can possibly make minor performance improvements, but you're never going to get away from the fact that the VM is going to take a while to start up and get going.
Make sure you haven't got the server VM selected for apps like this - that's one thing that really will increase the start up time.
The only real way round it is to compile Java to native code, which you can do with GCJ - so if you must write these apps in Java and you must have them faster, that might be a route to look down. Bear in mind though it's not that up-to-date and maintenance on it largely seems to be dying out too.
Haven't tried it yet but might be worth looking at nailgun. It will run your Java programs in the same JVM, so after "warming up" should be pretty fast. A "hello world" example goes from taking 0.132s to taking 0.004s
http://www.martiansoftware.com/nailgun/background.html
You can get a small speed-up with class data sharing https://rmannibucau.metawerx.net/post/java-class-data-sharing-docker-startup
A much bigger speedup should come from doing ahead-of-time compilation to a static binary using GraalVM native-image, although it's still tricky to use. A lot of libraries haven't been made compatible.
i came to know that java applications performance is also based on speed of the JVM that executing the byte code.So, I would like to know JVM speed while executing byte Code.Is this possible?
The JVM speed varies as it runs, i.e. it optimises the code it executes more the more often it is run.
You can write a micro-benchmark which you can measure and compare with other system.
Perhaps you could clarify why you need to know this?
first of all beware while reading some Java performance related material , you may find black, white, grey , depending from the creation date , the JVM used and so on...
Don't try to deliver overkilled applications , performance should remain a question of logic and should not induce to have a non human understandable code...
What do you mean with JVM speed ? JVM speed depends from many parameters:
* size of byte code
* performance of CPu used
* tuning of the OS and the JVM
* code you write
The main Java advantage remains portability (WORA acronym) so trying to write code behaving in different ways following one 'speed' parameter would be the worth thing to do ....
You may access to different of those parameters (JVM version, CPU , memory and so on) but to do what ? I totally agree with Peter Lawrey in this point....
I guess that you are a Java newcomer and you try to learn quickly , very good..
But try to put things in order....
Starting with writing code that works in a clear, robust and efficient way , easy to maintain is a very good starting point (life's work ????)
HTH
My 2 cents
Jerome
Best to use a profiler to work with,
Some reasons why your machine (probably jvm) works faster is due to a different power savings system your machine employs, eg, no bluetooth, wifi etc. However, this is disputable.
If you use Linux/Unix or any gnu tools, use the 'time' command, eg, time java classname to get the exact time it takes to execute the process.
But from my experience, I feel that I was more alert / productive working out of my office, hence seeing my laptop perform faster. perhaps its physiological.
Is it possible tell the JVM to hint the OS that a certain object should preferably not get pushed out to swap space?
The short answer is no.
Java doesn't allow you any control over what is swapped in and what is 'pinned' into RAM.
Worrying about this sort of thing is usually a sign that there is something else wrong in your project. The OS will on the whole do a much better job of working out what should be swapped and what shouldn't. Your job is to write your software such that it doesn't try to second guess what underlying VM/OS is going to do, just concentrate on delivering your features and a good design.
This problem has also been very noticeable in Eclipse and the KeepResident dirty hack plugin (http://suif.stanford.edu/pub/keepresident/) avoids it.
It might be a good place to start? I have not seen it in widespread use so perhaps this has been integrated in the standard Eclipse distribution?
Hey! You are programming in a managed language. Why are you thinking about these? If you can't get these stuff out of your mind, you can always choose to program in C.
The short answer is (as given above): Dont' do it :-).
It would however be possible in principle. Most OS do allow to "lock" certain memory areas from being swapped (e.g. mlock(2) under Linux, VirtualLock under Windows).
The VM could expose this functionality to Java applications via a suitable API. However, no VM I know of does that, so to do it, you would first have to modify your VM...
If you access it regularly, that whatever page it happens to be in at the time (the JVM moves stuff around during garbage collection) will not be paged out unless something else is requesting memory even more aggressively. But there is no way of telling the JVM to not move it to another page, and the OS only knows about pages.
Not an answer, but lacking points to comment, I reserve this option :)
There are reasons to not store information in swap. Be it passwords or other confidential information that should not spend eternity on disk. Also, coming back after a weekend to my pc, I'd like some things to be in memory immediately available.
(Non Java) Natively there is probably some way to do this for each/most operating systems. With windows this is definitely possible. But not straight out of java (think JNI).
Depending on how desperate this option is, you could always look at using video memory, or some other hardware device that does not swap out. This allows you to still use a standardish java api, like jogl to store information. But somehow I doubt that is in context with the implementation/results you are looking for.
Basically you want to keep the whole JVM in main memory the whole time.