0

When I learned Java, I put my all my files in the bin folder of my JDK folder. Then I compile my code in the command prompt I have to change the directory so that it goes to the bin folder. It looks like this:C:\Users\MdDaddyJr\Desktop\jdk1.7.0_51\bin>. Have I been putting the files in the wrong place the whole time? I am not using any IDEs.

James Bond
  • 5
  • 1
  • 1
  • 8
  • You can create a `workspace` directory somewhere in your user home directory and create new subdirecties for each project. Put your source files in there. Using the `bin` folder of the jdk is completely wrong. May work, but wrong. – Tom Nov 22 '14 at 02:05
  • are you using an IDE? Are you talking about your class files? Personally I have never put any files where you are.... I use netbeans maven projects typically. – Mark Giaconia Nov 22 '14 at 02:06

2 Answers2

0

You an put your files in any folder as you wish but its not good to keep it in the bin. But you have to set the path correctly to C:\Users\MdDaddyJr\Desktop\jdk1.7.0_51\bin. Then only you will be able to execute your program.
You can set the path by using this link https://www.java.com/en/download/help/path.xml

Johny
  • 2,128
  • 3
  • 20
  • 33
0

Sounds like you have not set up your environment correctly. Few points to start with:

  1. Add this C:\Users\MdDaddyJr\Desktop\jdk1.7.0_51\bin to your PATH environment variable - this will allow you to execute the javac and java commands from any folder, which means you don't need to change to the ../bin nor would you need to type the full path each time. So you can simply do:

    c:\Dev\workspace\project1>javac MyClass.java

  2. Create a dedicated folder where you place your Java sources and organise them in packages (i.e. subfolders) - see this exhaustive answer: What strategy do you use for package naming in Java projects and why?

  3. When compiling your files you need to be in the root folder where the package hierarchy starts. So for example, if your workspace is in C:\Dev\workspace\project1 and your class MyClass.java is in package my.study that is, it is in C:\Dev\workspace\project1\my\study\MyClass.java, then to compile it you do:

    c:\Dev\workspace\project1>javac my\study\MyClass.java

This will create the class file at C:\Dev\workspace\project1\my\study\MyClass.class, which you can then execute like so (assuming it has a main() method):

c:\Dev\workspace\project1>java my.study.MyClass

Of course such exercises are OK when dealing with isolated source files and to get a feel of the language. The moment you start writing some real-life programs with multiple inter-dependent classes and 3rd party JAR dependencies, the manual compilation above becomes extremely difficult to manage. For this, look at one of the popular Java IDEs: Eclipse, IntelliJ (community edition is free) or NetBeans.

Good luck.

Community
  • 1
  • 1
Pavel Lechev
  • 903
  • 8
  • 12