-1

In Java, is there a way to write a file without importing a class (e.g java.io). I know one way to do it would be to do something like this:

Runtime.getRuntime().exec("echo 'test' > test.txt ")

I am looking for other methods of achieving similar results.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
SivaDotRender
  • 1,581
  • 4
  • 21
  • 37
  • 7
    Why is it so important to not import anything? Import statements don't create any dependencies on libraries. They just allow you to omit the package name of classes that you import. – mhlz Nov 24 '15 at 15:19
  • 4
    Don't import anything and fully qualify all your classnames. No imports needed (although it will be much more verbose source code to produce identical compiled code to if you just used the import). – Tim B Nov 24 '15 at 15:22
  • 3
    Your requirement makes no sense though... – Tim B Nov 24 '15 at 15:22
  • 7
    Note that even your example does an (implicit) "import" - `Runtime` is part of the `java.lang` package which is automatically imported – Andreas Fester Nov 24 '15 at 15:22

2 Answers2

2

In Java, is there a way to write a file without importing a class (e.g java.io)

No, there is not. In Java, an import statement is not more than a shortcut for the Java Compiler to resolve unqualified names like String or Runtime so that you do not need to fully qualify all the names in the source code - after the compilation is done, in the .class file, there are only fully qualified references which include the package name.

Even your example

Runtime.getRuntime().exec("echo 'test' > test.txt ")

will become

java.lang.Runtime.getRuntime().exec("echo 'test' > test.txt ")

in the .class file:

$ javap -c Sample.class
  ...
  public static void main(java.lang.String[]) throws java.io.IOException;
    Code:
       0: invokestatic  #19                 // Method java/lang/Runtime.getRuntime:()Ljava/lang/Runtime;
       3: ldc           #25                 // String echo 'test' > test.txt
       5: invokevirtual #27                 // Method java/lang/Runtime.exec:(Ljava/lang/String;)Ljava/lang/Process;
       8: pop
       9: return

See also How java import works.

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
1

The proposition is absurd. It makes no sense to ask how to do something in [insert language here] without using the facilities that [insert language here] provides for the purpose.

In Java in particular, you can do nothing without relying on the standard library. Your class cannot even be loaded without use of at least one ClassLoader, and of every class that that ClassLoader depends on, recursively, including java.lang.String, java.util.Locale, java.io.InputStream, and many others. That you do not reference these explicitly in your code does not mean your program does not rely on them.

Moreover, it gains you nothing useful specifically to avoid import statements in your Java code, as those have no runtime manifestation: they are purely for programmer convenience.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157