Author Topic: [Bash] Script to compile and run simple Java Programs  (Read 516 times)

0 Members and 1 Guest are viewing this topic.

Offline chlortho

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 1
    • View Profile
[Bash] Script to compile and run simple Java Programs
« on: June 24, 2015, 03:40:12 pm »
Not sure if this belongs in Java or Scripting...

Anyhow, I am trying to write a script to compile and run simple java programs. Creating a batch file in DOS is a piece of cake:

Code: [Select]
javac %1.java
java %1
However, in BASH it's a bit more complicated (at least I think it is). Here is what I have so far, I feel like it's close. My issue (I think) is that there is not a -o switch with javac to specify the output file name. is there another way?

Code: [Select]

#!/bin/bash
# Script to compile and execute a jave program
 
# Get file name without the .java extension
file_name=$(echo $1|sed 's/\(.*\)\.java/\1/')
 
# Compile the program with -o option to specify the name of the binary
javac -o $file_name.out $1
 
# If there were no compilation errors, run the program
if [[ $? -eq 0 ]]; then
        ./$file_name.out
fi
« Last Edit: June 24, 2015, 04:12:00 pm by chlortho »
chlortho

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: [Python] Script to compile and run simple Java Programs
« Reply #1 on: June 24, 2015, 03:46:20 pm »
1. this is obviously not python, but a shellscript.
2. get that ugly ass font from my screen.
3. haven't tested it, but it looks useful (kinda).
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline chlortho

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 1
    • View Profile
Re: [Python] Script to compile and run simple Java Programs
« Reply #2 on: June 24, 2015, 04:11:32 pm »
1. this is obviously not python, but a shellscript.
2. get that ugly ass font from my screen.
3. haven't tested it, but it looks useful (kinda).


1. right. I'm retarded. I was working on a python script to configure my network manager for my university wifi. Must have been thinking about two things at the same time.
2. Yeah. It looks like Courier...  I cut and pasted from vim...  so not really sure.
3. You're right, it's 'kinda' useful - more of just to see if I could.


Excellent feedback, though. Thanks.
« Last Edit: June 24, 2015, 04:14:03 pm by chlortho »
chlortho

Offline chlortho

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 1
    • View Profile
Re: [Bash] Script to compile and run simple Java Programs
« Reply #3 on: June 24, 2015, 05:15:13 pm »
I don't think so? Should be as simple as:

Code: [Select]
javac $1.java
java $1


Holy crap...  That was it....  syntax %=CMD and $=Unix    wow.... thanks!  talk about over complicating things....haha
chlortho

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: [Bash] Script to compile and run simple Java Programs
« Reply #4 on: June 24, 2015, 10:15:15 pm »
I don't think so? Should be as simple as:

Code: [Select]
javac $1.java
java $1

+1 said what I came here to say
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women