r/javahelp • • Aug 19 '18

Question about JVM.

I have heard that Java is considered secure because the code actually runs in JVM and not on OS which is fine for most apps. For example if I am making a to-do app. But what if I want to make an app like CCleaner, it will need to delete some files (cache, junk files) from system. How will I be able to access system files as my code is contained inside JVM. If Java does allow me to access the system(read/write system files) via OS level API's then this statement that Java code runs only inside JVM and this is more secure than other programming languages is false.

If JVM internally checks what should and what should not be allowed to accessed then how is it different from, say python. Even their interpreter must be capable of making them as secure as Java.

0 Upvotes

5 comments sorted by

2

u/[deleted] Aug 19 '18

I have heard that Java is considered secure because the code actually runs in JVM and not on OS which is fine for most apps.

That's not why it's secure, the jvm maps to the os. It still runs on the os.

For example if I am making a to-do app. But what if I want to make an app like CCleaner, it will need to delete some files (cache, junk files) from system.

Java can access system files without any issues.

How will I be able to access system files as my code is contained inside JVM. If Java does allow me to access the system(read/write system files) via OS level API's then this statement that Java code runs only inside JVM and this is more secure than other programming languages is false.

Java code is converted to jvm bytecode. The jvm bytecode is run on the jvm. The jvm is run on the os. System calls in Java code as passed to the os by the jvm.

If JVM internally checks what should and what should not be allowed to accessed then how is it different from, say python. Even their interpreter must be capable of making them as secure as Java.

It isn't different than Python in that way, Python runs on a VM as well.

1

u/EncryptedGhostShadow Aug 19 '18

Thanks for the reply. What you say makes total sense. The thing is I have read something equivalent to "Java is secure because the code is contained/ trapped inside JVM l. So something like virus can never be created using Java that can harm your computer".

And I thought if this creates a Virtual Machine like stuff, it may not be able to do much changes to the system, so that will make Java a weak language. Because if it gives access it might not be able to differentiate between delete say junk files vs rm -f /.

3

u/feral_claire Software Dev Aug 19 '18

The "Java virtual machine" is a different type of virtual machine than you might normally think of. It doesn't create a sandboxed environment isolated from the OS. It's just translates java bytecode into machine instructions and doesn't provide any protection from malicious code messing up your system.

1

u/EncryptedGhostShadow Aug 19 '18

Thanks man. :)

1

u/OffbeatDrizzle Aug 21 '18

In terms of the security manager, you can set permissions up in the jdk's config files so that any app running under that specific JVM isn't allowed to, for example, read files. This is all implemented in code and can be seen inside the File class (java.io.File) where there are plenty of calls to the SecurityManager. The capabilities of the language extend out to the SecurityManager - they are not a given in the language itself