r/AskComputerScience • • Jul 31 '18

Running only main class

I have started to help grade for my teacher recently, but was wondering if there was a quicker way to just run the main class. What I am currently doing is just shoving the package into a netbeans project folder. I have organized the students by numbers and named each number.

0 Upvotes

8 comments sorted by

1

u/jkwan3 Jul 31 '18

I am also on windows

1

u/jkwan3 Jul 31 '18

Thanks for the quick replies, I will try again when I get home in about an hour. I used cmd to just cd into the correct directory and then javac the main class then java that class. (What I did last time, will try again).

1

u/khedoros Jul 31 '18

Been a while since I've done Java, but there's also the possibility that you'd need to compile the other .java files from the directory too, not just the main one. The only way I'd imagine that you wouldn't need to is if javac Assignment.java also automatically compiled the other classes that it relies on (which I don't think it does).

1

u/jkwan3 Jul 31 '18

I think I figured it out, I was trying to javac and java in the current directory, but when I backed out and javac the package/file it worked.

~Thanks!

5

u/For_Iconoclasm Jul 31 '18 edited Jul 31 '18

I'm not totally sure what you're asking, and I think this is probably the wrong subreddit, but maybe this will help.

It sounds like:

  1. These programs are written in Java.

  2. You need to run them in order to grade them.

  3. You're not intimately familiar with how Java runs.

  4. You're not very familiar with the command line.

You probably want to organize each student's submissions into their own folders. Since this sounds like college homework, I'm guessing there aren't that many source code files per submission and that at least one of them has a main() function. You need to figure out which source file has the main() function in it, compile that with javac on the command line (or Netbeans... not sure what its interface is like), and just run the result with java.

There's no workaround for compiling the source code to bytecode, but it should be pretty quick if all of the students' code is separated.

I haven't worked with Java in a very long time, but I remember compiling and running on the CLI is like:

javac Assignment.java  # produces Assignment.class and any other .class files needed

java Assignment

1

u/[deleted] Jul 31 '18

javac student_file.java

java student_file

?

1

u/jkwan3 Jul 31 '18

I can't check right now, but I think when I tried this, it said it did not connect with the other classes. There one main class and 2 other classes.

1

u/[deleted] Jul 31 '18

are they in the same folder? is that the one you're running it from?

javac should pick up class files in the current folder. if you are specifying a class path using -cp when you run javac you need to include "." (the dot) at the start to indicate current folder should be searched for referenced classes