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:
- Confirm JDK is installed correctly
- Check if JAVA_HOME environment variable is configured
- Check if Path environment variable includes %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Linux/macOS)
- 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:
- Ensure you're running the command in the correct directory
- Check if class name exactly matches the filename
- If class is in a package, use the full package name to run
- 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:
- Specify encoding during compilation: javac -encoding UTF-8 MyClass.java
- Specify encoding during runtime: java -Dfile.encoding=UTF-8 MyClass
- Ensure source files are saved as UTF-8
- 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:
- Check which JDK version JAVA_HOME points to
- On Windows, use where java to see all java command locations
- On Linux/macOS, use which java to see the path
- Adjust the order of JDK bin directories in PATH environment variable
- 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:
- Windows: netstat -ano | findstr 8080 to find process using the port
- Linux: lsof -i:8080 or netstat -tulpn | grep 8080
- 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:
- Increase JVM heap size: java -Xmx2048m MyClass
- Check code for memory leaks
- Use memory profiling tools (like VisualVM, JProfiler) to identify issues
- Optimize data structures to reduce memory usage
Common Development Issues
Issue 7: NullPointerException
Problem: NullPointerException is the most common runtime exception.
Solutions:
- Check if object is null before using it
- Use Optional class to avoid null pointers
- Use Objects.requireNonNull() to validate parameters
- Initialize all necessary objects
Issue 8: ConcurrentModificationException
Problem: Exception occurs when modifying a collection while iterating.
Solutions:
- Use iterator's remove() method to delete elements
- Use CopyOnWriteArrayList instead of ArrayList
- Record elements to delete first, then delete after iteration
- Use Stream API's filter to filter elements
Issue 9: ClassCastException
Problem: ClassCastException occurs.
Solutions:
- Use instanceof to check object type
- Prefer interface types over concrete implementations
- Perform type checking before casting
Issue 10: Number precision issues
Problem: Floating-point calculations are inaccurate.
Solutions:
- Use BigDecimal instead of double for precise calculations
- Understand floating-point precision limitations
- Avoid comparing floating-point numbers, use BigDecimal's compareTo
Performance Issues
Issue 11: Slow program execution
Problem: Java program runs slowly.
Solutions:
- Use performance profiling tools to locate bottlenecks
- Avoid repeated calculations in loops
- Use StringBuilder instead of string concatenation
- Choose appropriate collection types
- Enable JIT compiler optimizations
Issue 12: Deadlock
Problem: Program deadlock, cannot continue execution.
Solutions:
- Avoid nested locks
- Unify lock acquisition order
- Use tryLock() timeout mechanism
- Reduce synchronized block scope
Build Tool Issues
Issue 13: Maven/Gradle dependency conflicts
Problem: Dependency version conflicts during build.
Solutions:
- Use mvn dependency:tree to view dependency tree
- Explicitly specify dependency versions in pom/gradle
- Exclude transitive dependencies
- Use Maven Central's Bom to manage versions
Issue 14: Build failure
Problem: Maven or Gradle build fails.
Solutions:
- View error message to locate the problem
- Clean cache: mvn clean or gradle clean
- Check network connection
- Confirm JDK version meets project requirements
- Check for syntax errors
Framework Issues
Issue 15: Spring Boot startup failure
Problem: Spring Boot application cannot start.
Solutions:
- View error messages in startup logs
- Check application.properties or application.yml configuration
- Confirm database connection configuration is correct
- Check dependencies are complete
- Check if port is occupied
Issue 16: Hibernate LazyInitializationException
Problem: Accessing lazy-loaded objects after session is closed.
Solutions:
- Use @Fetch(FetchMode.JOIN) for eager loading
- Initialize required objects before session closes
- Use Hibernate.initialize() for manual loading
- 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:
- Master Java basic syntax and object-oriented concepts
- Familiarize with Java Collections Framework and common utility classes
- Learn JDBC database operations
- Master at least one Web framework (Spring/Spring Boot)
- Understand design patterns and code refactoring
- 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.
