-1

I have a few Java tests that I'd like to run by doing java <test-name>. But, all of the files are either .class or .java.

How do I trim off the end in a Bash script or in the terminal so that the command reads java <test-name>?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
makansij
  • 9,303
  • 37
  • 105
  • 183
  • 2
    FYI this question really has nothing to do with the Java programming language (the fact that your file ends in `.java` really has nothing to do with the question). I've edited the tags accordingly. – ajb Sep 07 '15 at 03:01
  • 1
    @ajb, On the contrary, it appears to be `bash` that is irrelevant to the question. It seems to be about the arguments to the `java` command, and in particular, the one that names the class in which to find the entry-point method. The arguments accepted by any command are the province of that command. Given that the question is open to interpretation, however, I leave it to the OP to restore the `java` tag if I have understood correctly. – John Bollinger Sep 07 '15 at 04:14
  • Maybe that's possible with bash tricks like @karakfa shows in his answer. But why? Sounds like you are setting yourself up for nasty pitfalls (not to mention any other people who work on the project). – Thilo Sep 07 '15 at 04:18
  • 1
    @Candic3, when running a Java program from the command line or from a script, you *never* include either a `.java` or a `.class` extension in the command. Instead, you give the fully-qualified name of the class containing the `main()` method you want to start in, something like `java my.tests.Test1`. The class must have previously been compiled, and you may need to also provide a `-classpath` argument to tell Java where to look for the class file. – John Bollinger Sep 07 '15 at 04:20
  • @JohnBollinger No, I think you've gotten it backwards. You're right that the `java` command controls what arguments it takes. And it takes an argument without the `java` extension. But the OP and everyone else already knew that--it wasn't the question. So your statement that the question is about the arguments to the `java` command is incorrect. The question is, when you're constructing the java command, how do you strip the `.java` extension off a file name--and that is most certainly a question about `bash` or other shells. – ajb Sep 07 '15 at 14:06
  • @ajb, only the OP can say, but my confidence in his level of knowledge is less than yours, in part because he implies that he thinks that `.java` files (as opposed to `.class` files) have any runtime relevance at all. Anyway, it doesn't matter. He'll need to ask a new, better formulated question if what he got from this one wasn't what he was looking for. – John Bollinger Sep 07 '15 at 14:13

1 Answers1

0

If you're using bash you can do the following

$ file="path/Foo.class"
$ echo "${file%.class}"
path/Foo
karakfa
  • 66,216
  • 7
  • 41
  • 56