9 Replies - 440 Views - Last Post: 30 March 2012 - 02:49 PM Rate Topic: -----

#1 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Command Line compiling/Jarring

Posted 29 March 2012 - 09:16 PM

As per pbl's request, I dove into manually compiling my applications.

However I have been plagued with error after error while trying to get it to compile and run in my command prompt.
This is the latest, and most puzzling problem I've ran into yet:

I created a "Hi There!" program using Windows notepad:
package hiThere;

public class HiThere {

	public static void main(String[] args) {
		System.out.println("Hi There");
	}

}


Simple. It's created on my desktop in:
C:\Users\Achri\Desktop\hiThere

//my cmd prompt
C:\Users\Achri\Desktop\hiThere>javac HiThere.java

C:\Users\Achri\Desktop\hiThere>

C:\Users\Achri\Desktop\hiThere>java HiThere
Exception in thread "main" java.lang.NoClassDefFoundError: HiThere (wrong name:
hiThere/HiThere)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)


C:\Users\Achri\Desktop\hiThere>


I've spent some time searching and I think the problem is my CLASSPATH variable.

because when I go up a level:
C:\Users\Achri\Desktop\hiThere>cd..

C:\Users\Achri\Desktop>java hiThere.HiThere
Hi There

C:\Users\Achri\Desktop>

So I went in and I didn't even have a CLASSPATH variable in my environment variables (Windows 7).

I added a variable named CLASSPATH with a Value of .;
So now when I perform an echo %CLASSPATH%, I receive .; instead of %CLASSPATH%.

Now when I go back into C:\Users\Achri\Desktop\hiThere I still see the same error? Is adding .; as CLASSPATH, not doing what I was expecting? (I was expecting it to let me run my files from the folder they are sitting in)

Is This A Good Question/Topic? 0
  • +

Replies To: Command Line compiling/Jarring

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,177
  • Joined: 06-March 08

Re: Command Line compiling/Jarring

Posted 29 March 2012 - 09:24 PM

Never say you have to compile line mode :)

Anyhow

C:\Users\Achri\Desktop\hiThere>java HiThere

should be

C:\Users\Achri\Desktop>java hiThere.HiThere

Never say you have to compile line mode :)


If you are a beginner avoid package if you don't really need them

*Edited: remove duplicated text coming from ???

This post has been edited by pbl: 29 March 2012 - 09:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: Command Line compiling/Jarring

Posted 29 March 2012 - 09:34 PM

View Postpbl, on 29 March 2012 - 09:22 PM, said:

Never say you have to compile line mode :)

Anyhow

C:\Users\Achri\Desktop\hiThere>java HiThere

should be

C:\Users\Achri\Desktop>java hiThere.HiThere


lol My sentence should have read:
As per pbl's request, I dove into manually jarring my applications, but because I couldn't get the manual jarring to work, I thought that maybe I should be manually compiling them as well, thinking that Eclipse was negatively impacting my .class files.


You always have to be one level above your package folder to manually run your file?
So if I were to always manually compile files from the eclipse directories:
C:\Users\Achri\workspace\ProjectName\src\PackageName\*.java

I would need to be in:
C:\Users\Achri\workspace\ProjectName\src\


and use
java PackageName.ClassName
to compile? Is this how everyone does it then?

Edit: I have always used packages, because Eclipse seems to sit in the corner and frown for using the default package.

This post has been edited by Achri: 29 March 2012 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,177
  • Joined: 06-March 08

Re: Command Line compiling/Jarring

Posted 29 March 2012 - 09:43 PM

Ignore Eclipse warnings :)
I cut and paste code from that forum into Eclipse 20 times a day to fix programs. Imagine I would have to create a package everytime ? Actually if the code that I cut an paste has a package statement, I simply delete it :)

But, continue JARing by hand :^:
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5433
  • View blog
  • Posts: 8,747
  • Joined: 19-March 11

Re: Command Line compiling/Jarring

Posted 29 March 2012 - 10:06 PM

Just so you know what the specific problem is, package declarations require a particular directory structure.

package foo;
public class Bar{
}


at the head of your code means that java will look for the file Bar.class in a directory called foo. This can lead to all sorts of confusion and in my opinion it's a piece of java that's not implemented very well - or perhaps it's implemented well and I don't understand it properly, that's possible as well.

Packages are very useful in more advanced programs, because they allow you to structure your code at an even higher level than the class file - and this is certainly why they are so closely tied to the directory structure. At that level, you'll want to be working with a build tool called ant, which will make your compile/test/build/deploy process much easier.

For now, though, just omit the package declaration and all will be well.

This post has been edited by jon.kiparsky: 29 March 2012 - 10:07 PM

Was This Post Helpful? 1
  • +
  • -

#6 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: Command Line compiling/Jarring

Posted 29 March 2012 - 10:13 PM

Thank you both for your well written answers.

I removed my packaging on my Mancala game I've been working on. And now I compiled and jarred it.
Last question for the night:

Why does this work:
C:\Users\Achri\Desktop\Mancala>java -jar Mancala.jar

but double clicking it, pops up

"Could not find the main class: MancalaMain. Program will exit." ?

My manifest file inside of the .jar:
Manifest-Version: 1.0
Created-By: 1.7.0_02 (Oracle Corporation)
Main-Class: MancalaMain




It shouldn't be the manifest file since I can run the program on the command line.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,177
  • Joined: 06-March 08

Re: Command Line compiling/Jarring

Posted 30 March 2012 - 03:53 AM

Weird :) You sure you doubble click the same .jar ?
No the one on your Desktop but the one in the folder /Mancala which is on your Desktop ?
Was This Post Helpful? 0
  • +
  • -

#8 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: Command Line compiling/Jarring

Posted 30 March 2012 - 10:39 AM

Command Prompt:
C:\Users\Achri\Desktop\Mancala>java -jar Mancala.jar

Starts my application, the JFrame window with the JPanels pops up.

Window:
C:\Users\Achri\Desktop\Mancala

dclicking the Mancala.jar gives me the error:
"Could not find the main class: MancalaMain. Program will exit.


Since I created the .jar at the command prompt and the manifest is like so:
Manifest-Version: 1.0
Created-By: 1.7.0_02 (Oracle Corporation)
Main-Class: MancalaMain



When I double click, I am trying to open a .jar created using Java 1.7, but what Windows is actually using is still 1.6?
And the command prompt just happens to know what to use?

So should I somehow figure out how to force Windows to be using my Java 1.7 build, instead of the 1.6? I ran into a similar problem when I was creating .JARs in Eclipse, I had to change the compliance level to 1.6 or lower to dclick them outside of eclipse.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5433
  • View blog
  • Posts: 8,747
  • Joined: 19-March 11

Re: Command Line compiling/Jarring

Posted 30 March 2012 - 10:44 AM

The 1.7 release seems to be causing more trouble than usual. You might see if you can get up to 1.7 jre on your machine, or else cut back to 1.6 for your jdk. I'm not using 1.7 yet, it doesn't look like there's anything very exciting there for me and the headaches seem unwarranted. I'll be a late adopter on this one.
Was This Post Helpful? 1
  • +
  • -

#10 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: Command Line compiling/Jarring

Posted 30 March 2012 - 02:49 PM

View Postjon.kiparsky, on 30 March 2012 - 10:44 AM, said:

The 1.7 release seems to be causing more trouble than usual. You might see if you can get up to 1.7 jre on your machine, or else cut back to 1.6 for your jdk. I'm not using 1.7 yet, it doesn't look like there's anything very exciting there for me and the headaches seem unwarranted. I'll be a late adopter on this one.


Yes, I installed the 1.7 JRE and that fixed things(I can dclick 1.7 JARs and they run). I still see my 1.6 JRE in the 32 bit directory after installation though. At least I can run my programs made w/ 1.7 now. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1