0

Is it possible to compile and run a java file in bash without saving the compiled file? I'm using the command javac File.java && java File. It works but it creates a File.class file.

Can I somehow pipe the compiled file to the java command? I have tried javac File.java | java but it does not work.

3
  • Could you explain the use case for this approach? Commented Nov 5, 2016 at 20:45
  • @StefanFreitag Thanks for your comment. The reason why I wanted to do this is that I don't want to have useless files in my file system and I thought there would be an easier way than deleting it everytime. Commented Nov 5, 2016 at 21:04
  • You can compile java code within a running JVM then load the compiled class and run it. see: quora.com/… Commented Nov 6, 2016 at 10:17

2 Answers 2

1

To my knowledge, this is not possible, and it also doesn't make much sense.

You see, your JVM comes with many megabytes of precompiled classes; and many of them will be loaded while running your small class. There is no point in saving the 1 K or whatever your File.class consumes on disk space.

So you should step back and re-think what kind of problem you think you can solve by such an approach.

Sign up to request clarification or add additional context in comments.

Comments

1
javac File.java && java File ; rm File.class

1 Comment

this. although like above I gotta ask "Why?" Would you compile /bin/bash every time you needed to start a shell process?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.