Running Your Code6:58 with Craig Dennis
Let's explore Run Configurations and how to kick off our awesome projects.
So now that we know how to get our code into the editor, 0:00 let's take a quick look at how to execute it. 0:03 In WorkSpaces we built our application by manually kicking off the compiler, 0:05 Java C, which created Java class files containing the byte code. 0:09 Then we ran the Java executable and 0:13 passed in the name of the class that contained our starting point. 0:15 You know, the static main method. 0:18 Well I'm glad that we learned how to do that it will help 0:20 demystify the compilation and running process that you are about to see. 0:23 The IDE does all that and more. 0:27 Ready to learn how? 0:29 Let's go take a look. 0:30 Okay so the first thing to remember is that you can have multiple modules and 0:33 therefore it's possible that you can have static main methods in 0:36 any of those files that you want to run. 0:40 And there's no real way that the ID could possibly know which file 0:41 you wanna execute. 0:45 So the idea in running something is to first find the file that you wanna run. 0:47 So, I know that our file that we ran before was called Karaoke. 0:51 And I know it's a little confusing it's the Karaoke project. 0:56 It's the Karaoke file. 0:58 There's also a karaoke machine. 0:59 We should probably rename that in the future sometime. 1:01 For now let's open it. 1:03 And let's say that I didn't want to use the mouse to click over there. 1:04 If I come over here and 1:10 press double shift, there's a great tool called Search Everywhere. 1:11 I'm just going to start typing karaoke. 1:15 There it is, classes, I'm going to choose that and I'm going to press Enter, boom. 1:17 There's our Karaoke file, great. 1:21 This is what I want to run, right? 1:24 Remember this goes and starts every thing off. 1:25 So since I have it open, I'm going to go to the run menu, and 1:27 I'm just going to choose run here. 1:31 Now it's just double-checking what I meant, and 1:34 it's gonna do this here, Karaoke, and I'm gonna click this. 1:37 It's gonna go ahead and it's gonna build the file. 1:41 After it's built, we'll take a look at how it got ran. 1:44 Okay, so it's gonna pop up a new window here called the console. 1:48 And here, you'll see that the program's actually running and it's waiting for 1:52 our input. 1:56 Here's the program that got kicked off and 1:57 I just kinda wanna show you all the way over here to the end. 1:59 That's kind of a crazy set up, but it's running basically what we did, 2:02 but it's wrapping it in this IDE magic and what that does is it allows us to. 2:07 Type into this window, and it allows system out to come into here. 2:12 So right, so system in is coming in and capturing it, and it's wrapping it. 2:14 But it's still passing at this parameter here. 2:18 So let me scroll all the way back. 2:24 Okay, cool. 2:25 So, what do we wanna do? 2:28 Let's choose a song to sing. 2:29 Right? 2:31 Let's choose YouTube. 2:32 And I still haven't found what I'm looking for. 2:34 Okay, great. 2:38 So it's pretty cool right? 2:39 Now the bummer is that in a few of our apps we used 2:41 system.console which is specifically made for console apps by the Java team. 2:44 So the bad news is. 2:50 And you might have heard some chatter about this in the forum. 2:51 The don't work so well in this redirected IO model that most of these IDEs use. 2:53 Right? So the way that they're writing to 2:58 the window and reading from the window. 2:59 So currently there are bugs filed in almost all of the popular IDEs that I saw 3:01 to support the system.console bug, but the big problem is, 3:06 is that console is marked final. 3:10 Remember that can't be changed. 3:11 So it's pretty hard for these IDE maintainers to get things right. 3:12 So you can't really blame them. 3:15 Maybe by the time you're watching this they'll already have it fixed. 3:18 But if not, use the tricks that we use in this Karaoke app of using system out and 3:20 system in as opposed to system dot council. 3:25 I've included some notes about the problem in the teachers' notes. 3:28 Now, since we've run this once what happened was it 3:32 created a run configuration. 3:34 So you'll see here in the menu that it has a Run Karaoke. 3:37 Let's go ahead and click that again. 3:40 So it's kicking it off again, and one thing that I really wanted to point out is 3:43 that we left our first application still running over here, and 3:47 there's another one running. 3:50 So, it's running two. 3:52 And that's totally fine, but 3:53 you might forget and get a whole bunch of these going. 3:55 So it's important to know that there's a little run menu over here, and 3:57 you can stop things. 4:00 So let's stop that Karaoke. 4:01 Let this process finish, and we'll come over here. 4:03 The other thing you could do is say Close, or Close Tab or Close All Tabs. 4:07 And this is a pretty common thing to see. 4:12 The Karaoke is running, we're just gonna say to disconnect and 4:15 terminate the process. 4:18 Boom, and now, well, our menu's gone from down there. 4:18 Okay, so this is the runbar up here. 4:21 You'll also see that 4:25 all the different configurations that got built will be shown in here. 4:27 So we just built that configuration for Karaoke so it's in here. 4:30 So, let's go ahead and open up that configuration. 4:33 Okay, cool. So these are the settings for it. 4:38 You can set options for each time that the program runs. 4:40 So remember in our Hangman app, we passed the name of the puzzle. 4:42 That's what this Program arguments field is here. 4:46 The Working directory is what is set from where things start. 4:48 So if you remember, that's why we dropped the songs.txt. 4:52 Right into the content route because this is the content route right. 4:55 Karaoke, content route. 4:58 So, that's where things work relatively. 5:00 I don't know if you remember exactly where that is but 5:03 I want to show you where we're doing this. 5:06 So I know that we're doing this thing here. 5:08 Called import from songs.text. 5:11 So let's say that we didn't know where that songBook def was. 5:14 But we knew that the function was called import from. 5:17 So you can also sort modules right. 5:18 So if I go import from and I hit it. 5:21 It will go right to where it was at. 5:24 And let's see. 5:26 So it says a new import stream reader on the fileName and 5:28 that was songs.text that we passed in and since. 5:31 This thing works relatively. 5:33 It's working from the working directory, which is right here. 5:34 Cool right? 5:38 Okay, so let's flip back really quick to that configuration. 5:40 So I'm going to click this up here and choose that Configurations. 5:42 I want to show you one more thing because it's kind of buried and 5:44 I've seen some questions about this so I just want to make sure I point it out. 5:48 Logging is a great tool that is available in Java and 5:51 Java has a great logging system. 5:54 It allows you to categorize and place entries of different severities and 5:56 lets you know what's happening during your application. 6:00 And maybe you don't want to show users what's happening during your application, 6:02 but you do want to have logs of what was happening. 6:05 So, there's a tab over here that says Logs and you can configure them here. 6:08 You can save everything that happened to a console to a file. 6:11 So, that's kind of helpful through debugging, 6:13 if you wanted to get back to it later. 6:15 And there's also the ability to show the console when it changes, 6:17 which is also pretty nice. 6:22 So the last thing that I want you to remember here is that you can have 6:24 multiple modules with run configurations, and just remember that pressing 6:27 this run button here will run whatever is in this list here. 6:32 So this is Karaoke. 6:35 So if I press play, it will run that specific configuration. 6:36 Great, all right. 6:40 So now we can navigate around our file structure, and build and 6:42 run our application. 6:45 There is a ton more of this ID you can do, 6:46 and much more than I could possibly show you in this course. 6:49 Let's spend a little time learning how to figure more out, and 6:52 also let me show off a few of my favorite tricks. 6:55
You need to sign up for Treehouse in order to download course files.Sign up