0

In general, how can one execute a .class file through powershell/command prompt that contains packages in it? I'm still having trouble with that. Assume the file is ClassFile.class, with the package called Pack and the class file's location in C:\Program Files\Folderhere\ClassFile.class.

brandbest1
  • 416
  • 1
  • 5
  • 11
  • does the class have a `main`? – Jesus Ramos Jun 25 '13 at 21:22
  • Well I do see "main method" in the code, so i assume so. Sorry, I am a big noob when it comes to Java. – brandbest1 Jun 25 '13 at 21:33
  • Then you can just do `java ClassFile` without the `.class` extension. At least in linux. In windows I can't remember if the java path is available for execution from the command line easily. – Jesus Ramos Jun 25 '13 at 21:34
  • It's giving me the `NoClassDefFoundError` error. I have my classpaths and paths all changed, but it still gives me the problem. – brandbest1 Jun 25 '13 at 21:41

1 Answers1

2

If the class is in a package called Pack then you'd do

java Pack.ClassFile

Have you copied the compiled code around at all, moving it from one directory to another? If you have, it's important to maintain the directory structure that it was originally compiled into.

If I was writing some code in a package and compiling and running it, these are the steps I'd take. Assume we've cd'd to a clean directory first.

mkdir Pack
vi Pack/ClassFile.java
javac Pack/ClassFile.java
java Pack.ClassFile
DaveH
  • 7,187
  • 5
  • 32
  • 53
  • Nope, haven't changed any directories. I tried what you suggested and I got this message : `Could not find or load main class Pack.ClassFile` – brandbest1 Jun 25 '13 at 21:51
  • OK - I've expanded the answer a bit so you can see the general steps that need to be taken to comile and run code in packages – DaveH Jun 25 '13 at 22:02