• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Command Line Compiling & Running

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://ibb.co/fdv977h
^ This is where you can see everything i've done

I typed compiled the files properly and was able to view them in the package folders i created.
However when I tried to run the class I got an error
java.lang.NoClassDefFoundError: ClassB (wrong name: packageb/ClassB)
 
Master Rancher
Posts: 4700
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class seems to be in a package called packageB.
Assuming it's in a folder called packageB, then you should be in the folder that contains packageB when in the command line.
Then the run command would be:
java packageB.ClassB
 
Nick Deluca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://ibb.co/QM1ns9n

So I typed cd packageb

However I still get the same error after trying to run the file.
 
Sheriff
Posts: 15936
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't declared the package in ClassB. Try adding a package declaration as your first line. Without it, ClassB will be in the default package.
 
Nick Deluca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You haven't declared the package in ClassB. Try adding a package declaration as your first line. Without it, ClassB will be in the default package.



You mean like this?
https://ibb.co/RY4R7sZ

Because I still get the same error.

Just to clarify, I'm using JDK SE 11 so the commands should be correct.
 
Marshal
Posts: 7788
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see you compiled the class, but you are actually trying to run it.

What's your intention?
 
Nick Deluca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I don't see you compiled the class, but you are actually trying to run it.

What's your intention?



It is compiled.

My intention is just to run it and see if it works, in this case it should print out Hello World.

I'm just trying to get to know how command lines work. And according the book i'm reading the command I put should work.
 
Liutauras Vilda
Marshal
Posts: 7788
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run it you need to be one level up, so if you do ls (unix) or dir (windows?) you'd see packageb directory.
 
Liutauras Vilda
Marshal
Posts: 7788
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please show us an evidence you have a compiled class within packageb directory.
 
Liutauras Vilda
Marshal
Posts: 7788
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my evidence:

1.
~/Downloads/test/packageb ls
ClassB.class ClassB.java


2.
~/Downloads/test pwd
/Users/liutaurasvilda/Downloads/test


3.
~/Downloads/test java packageb.ClassB
Hello World
 
Nick Deluca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://ibb.co/z54RDPY

This is the directory and the files ^ in which you can see that there is indeed a class file.

I then went to the Temp folder attempted java packageb.ClassB  which lead me to the same error.
 
Liutauras Vilda
Marshal
Posts: 7788
536
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By looking to the timestamps of the *.java and *.class files, I'd guess that after you got advice to add packageb to your source code file (*.java), you never recompiled that class.
 
Nick Deluca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup turns out I needed to recompile, which is weird considering I thought I already did...
Guess it was just a mistake on my end. Thanks!
 
Liutauras Vilda
Marshal
Posts: 7788
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nick Deluca wrote:Guess it was just a mistake on my end.


Your guess is correct

Nick Deluca wrote:Thanks!


You're most welcome

 
Junilu Lacar
Sheriff
Posts: 15936
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nick Deluca wrote:Yup turns out I needed to recompile, which is weird considering I thought I already did...
Guess it was just a mistake on my end. Thanks!


You need to recompile every time you change your source code if you're going to run your program like that.

The other way to run it is in single file source mode where you just do this directly: java packageb/ClassB.java

If you run it this way, the program will be compiled in memory and there won't be .class file created in the file system by the compilation process.
 
Marshal
Posts: 70654
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't that require all your code be in one XXX.java file?
 
Junilu Lacar
Sheriff
Posts: 15936
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a simple Hello World program.
 
Campbell Ritchie
Marshal
Posts: 70654
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can sense a challenge: see how many unnecessary classes you can divide a Hello World into
 
Junilu Lacar
Sheriff
Posts: 15936
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knock yourself out
 
Liutauras Vilda
Marshal
Posts: 7788
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

As a side note to take home, is the importance of having timestamps wher things can be created and updated. That is almost always a useful information to capture and later use it if/once the need comes.

i.e.

1. user's billing information created - all works for years
2. all of a sudden things stop working (assuming issue is in billing information)
3. by checking billing information's updated at timestamp you could verify whether information was updated. By whom updated? Update reason? etc...

The more metadata information you have around you, the easier is to understand the matter.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic