• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to specify the package path from the command line to run an application?  RSS feed

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The project source file created using an IDE is in the following location

The Project folder is HelloWorld.it has an src folder.The src folder has the package path folders com.test and in the test folder is the HelloWorld.java.The class HelloWorld is in com.test package.By going through the command line the class HelloWorld.java is successfully compiled But it can not be run. I am getting the exception "Caused by: java.lang.ClassNotFoundException".

I used the commands, But no success.Even the complete path to the source file was used but the error "java.lang.ClassNotFoundException" keeps coming.The OS is Windows 10


 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only use com.test as your package name if you own the web address test.com.

Where are you trying to find those files from? You shou‍ld be in such a location that com is a subdirectory of your current directory. Assuming that isn't the problem, and that you haven't caused problems by setting a system CLASSPATH:

The name of the class is no longer HelloWorld; it is com.test.HelloWorld. No space after the package name, but a . after the package name.
 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Only use com.test as your package name if you own the web address test.com.

Where are you trying to find those files from? You shou‍ld be in such a location that com is a subdirectory of your current directory. Assuming that isn't the problem, and that you haven't caused problems by setting a system CLASSPATH:

The name of the class is no longer HelloWorld; it is com.test.HelloWorld. No space after the package name, but a . after the package name.



Where are you trying to find those files from?
        When the post was made I was in the test folder But after seeing your statement You shou‍ld be in such a location that com is a subdirectory of your current directory., I came to the src folder.The src folder has com and test folders.

Just now from within the src folder I used the command java com.test HelloWorld But the result is, then the command but the same result



 
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java com.test.HelloWorld

But, is that folder (the src folder) where the IDE put the class files?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write com.test twice. I have told you no space after the package name, only a dot.

If you have separate src and bin folders, the .class files will be in a different location. Don't try to mix IDE folders and folders created with mkdir. Well, not until you have lots of experience. If the .class files are in a bin folder, you shou‍ld run them from there.
 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This
java com.test.HelloWorld
works and gives the output
Hello, World!


The name of the class is no longer HelloWorld; it is com.test.HelloWorld. No space after the package name, but a . after the package name

        I understood the above statement as if you were meaning that the name of the class should be com.test.HelloWorld and as if the package name must also be specified. I couldn't understand this
No space after the package name


If you have separate src and bin folders, the .class files will be in a different location. Don't try to mix IDE folders and folders created with mkdir. Well, not until you have lots of experience. If the .class files are in a bin folder, you shou‍ld run them from there.

        I went into the test folder where the source file is located and compiled it first, so there is a separate .class file from the one generated by the IDE in the test folder. I just wanted to try things, that's why tried this

So the solution is: type the package name and the name of the file to be run separated by dots, and no separate .class file is to be  specified

Thanks, Campbell Ritchie




 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:java com.test.HelloWorld



But, is that folder (the src folder) where the IDE put the class files?


The
java com.test.HelloWorld
works

The IDE puts the .class files in a folder called out.The IDE is IntelliJ IDEA. I went into the src\com\test  and compiled the source file through the command line
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varuna Seneviratna wrote:I went into the src\com\test  and compiled the source file through the command line


Ah right, that'll be why it worked then.
I thought you were trying to run the code the IDE had compiled which, as you say, would have been in a different directory.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yesterday, I wrote:. . . . Don't try to mix IDE folders and folders created with mkdir. Well, not until you have lots of experience. . . .
I still think that is good advice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!