Java Frequently Asked Questions

Basic Issues

Issue 1: java and javac commands not found

Problem: Entering java -version or javac -version in command line shows command not found.

Solutions:

  1. Confirm JDK is installed correctly
  2. Check if JAVA_HOME environment variable is configured
  3. Check if Path environment variable includes %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Linux/macOS)
  4. Reopen terminal to apply environment variable changes

Issue 2: Could not find or load main class

Problem: Running java command shows "Could not find or load main class".

Solutions:

  1. Ensure you're running the command in the correct directory
  2. Check if class name exactly matches the filename
  3. If class is in a package, use the full package name to run
  4. Check if classpath is set correctly

Issue 3: Encoding issues causing garbled Chinese characters

Problem: Chinese characters in program output appear as garbled text.

Solutions:

  1. Specify encoding during compilation: javac -encoding UTF-8 MyClass.java
  2. Specify encoding during runtime: java -Dfile.encoding=UTF-8 MyClass
  3. Ensure source files are saved as UTF-8
  4. Set correct file encoding in IDE

Environment Configuration Issues

Issue 4: Multiple JDK version conflicts

Problem: System has multiple JDK versions, java command uses the wrong version.

Solutions:

  1. Check which JDK version JAVA_HOME points to
  2. On Windows, use where java to see all java command locations
  3. On Linux/macOS, use which java to see the path
  4. Adjust the order of JDK bin directories in PATH environment variable
  5. Use update-alternatives (Linux) to manage multiple versions

Issue 5: Port already in use

Problem: Starting Java web application shows port is already in use.

Solutions:

  1. Windows: netstat -ano | findstr 8080 to find process using the port
  2. Linux: lsof -i:8080 or netstat -tulpn | grep 8080
  3. Kill the process using the port, or change the application's port configuration

Issue 6: Out of Memory Error

Problem: Program throws OutOfMemoryError during runtime.

Solutions:

  1. Increase JVM heap size: java -Xmx2048m MyClass
  2. Check code for memory leaks
  3. Use memory profiling tools (like VisualVM, JProfiler) to identify issues
  4. Optimize data structures to reduce memory usage

Common Development Issues

Issue 7: NullPointerException

Problem: NullPointerException is the most common runtime exception.

Solutions:

  1. Check if object is null before using it
  2. Use Optional class to avoid null pointers
  3. Use Objects.requireNonNull() to validate parameters
  4. Initialize all necessary objects

Issue 8: ConcurrentModificationException

Problem: Exception occurs when modifying a collection while iterating.

Solutions:

  1. Use iterator's remove() method to delete elements
  2. Use CopyOnWriteArrayList instead of ArrayList
  3. Record elements to delete first, then delete after iteration
  4. Use Stream API's filter to filter elements

Issue 9: ClassCastException

Problem: ClassCastException occurs.

Solutions:

  1. Use instanceof to check object type
  2. Prefer interface types over concrete implementations
  3. Perform type checking before casting

Issue 10: Number precision issues

Problem: Floating-point calculations are inaccurate.

Solutions:

  1. Use BigDecimal instead of double for precise calculations
  2. Understand floating-point precision limitations
  3. Avoid comparing floating-point numbers, use BigDecimal's compareTo

Performance Issues

Issue 11: Slow program execution

Problem: Java program runs slowly.

Solutions:

  1. Use performance profiling tools to locate bottlenecks
  2. Avoid repeated calculations in loops
  3. Use StringBuilder instead of string concatenation
  4. Choose appropriate collection types
  5. Enable JIT compiler optimizations

Issue 12: Deadlock

Problem: Program deadlock, cannot continue execution.

Solutions:

  1. Avoid nested locks
  2. Unify lock acquisition order
  3. Use tryLock() timeout mechanism
  4. Reduce synchronized block scope

Build Tool Issues

Issue 13: Maven/Gradle dependency conflicts

Problem: Dependency version conflicts during build.

Solutions:

  1. Use mvn dependency:tree to view dependency tree
  2. Explicitly specify dependency versions in pom/gradle
  3. Exclude transitive dependencies
  4. Use Maven Central's Bom to manage versions

Issue 14: Build failure

Problem: Maven or Gradle build fails.

Solutions:

  1. View error message to locate the problem
  2. Clean cache: mvn clean or gradle clean
  3. Check network connection
  4. Confirm JDK version meets project requirements
  5. Check for syntax errors

Framework Issues

Issue 15: Spring Boot startup failure

Problem: Spring Boot application cannot start.

Solutions:

  1. View error messages in startup logs
  2. Check application.properties or application.yml configuration
  3. Confirm database connection configuration is correct
  4. Check dependencies are complete
  5. Check if port is occupied

Issue 16: Hibernate LazyInitializationException

Problem: Accessing lazy-loaded objects after session is closed.

Solutions:

  1. Use @Fetch(FetchMode.JOIN) for eager loading
  2. Initialize required objects before session closes
  3. Use Hibernate.initialize() for manual loading
  4. Configure spring.jpa.open-in-view=true

Version Selection Issues

Issue 17: Which Java version should I learn

Recommendations:

  • Beginners: Java 21 or Java 17 recommended, modern syntax, abundant resources
  • Enterprise projects: Based on project dependencies, usually Java 8, 11, or 17
  • New projects: Strongly recommend Java 21 or higher

Issue 18: Difference between JDK and JRE

Answer:

  • JDK (Java Development Kit): Development kit, includes JRE and compilation/debugging tools
  • JRE (Java Runtime Environment): Runtime environment, only contains class libraries and JVM needed to run Java programs

Install JDK if developing Java programs. JRE is sufficient if only running compiled Java programs.

Other Common Issues

Issue 19: How to learn Java

Recommendations:

  1. Master Java basic syntax and object-oriented concepts
  2. Familiarize with Java Collections Framework and common utility classes
  3. Learn JDBC database operations
  4. Master at least one Web framework (Spring/Spring Boot)
  5. Understand design patterns and code refactoring
  6. Practice more, write projects

Issue 20: How long will Java remain relevant

Analysis: As a mainstream language in enterprise development, Java has a mature ecosystem and large user base. According to TIOBE rankings, Java consistently ranks in the top three. Java continues to be updated, with new versions released every six months and LTS versions every two years. Therefore, Java will remain a valuable skill in the foreseeable future.