Tag Archives: java

How to Solve JAVA Test Class Error: java.lang.nullpointerexception

 

1. Error information:

IDEA:

Java test class java.lang.nullpointerexception

2. Error reporting reason:

Without annotation, the unit test cannot call other classes in the project.

3. Error reporting solution:

These two annotations should be used above the test class:

@RunWith(SpringJUnit4ClassRunner.class)

  @SpringBootTest

Java calls sqlldr and reports an error: Message 2100 not found

Java calls the sqlldr command of Oracle and reports an error: Message 2100 not found; No message file for product=RDBMS, facility=ULMessage 2100 not found; No message file for product=RDBMS, facility=UL

Manually executing sqlldr can be executed normally, but if it is placed in Java, the above 2100 error will be reported

I’ve been looking for mistakes for two days

Hope to help and record this problem

Sort out the solutions (two kinds) here (first say your own, and then summarize what Baidu has found)

1. Let’s talk about my problems first

Let’s start with the conclusion: you can execute sqlldr manually, but you need to set environment variables in Java( I don’t know much about Java. At least the problem will be solved after I set it like this.)

The line of runtime. Getruntime(). Exec (CMD) found on the Internet can execute sqlldr. The method is correct, but it seems that it has not been written, and the environment variable needs to be set

 

String[] cmd = { "/bin/bash", "-c", "echo $ORACLE_HOME;echo $LD_LIBRARY_PATH;$ORACLE_HOME/bin/" + shellCommand };

final Process pid = Runtime.getRuntime().exec(cmd,new String[] { "ORACLE_HOME=/oracle/app/oracle/product/11.2.0.4/dbhome/",
            "LD_LIBRARY_PATH=/usr/local/lib:/oracle/app/oracle/product/11.2.0.4/dbhome/lib:$LD_LIBRARY_PATH" });
            Process pid = Runtime.getRuntime().exec(cmd
            System.out.println("Process pid" + pid);
            bufferedReader = new BufferedReader(new InputStreamReader(pid.getErrorStream()), 1024);

Just add these lines

The full code is as follows:

public static int executeShellsetHome(String shellCommand) {
        int success = -1;
        BufferedReader bufferedReader = null;
        String[] cmd = { "/bin/bash", "-c", "echo $ORACLE_HOME;echo $LD_LIBRARY_PATH;$ORACLE_HOME/bin/" + shellCommand };
        try {
            System.out.println("run  executeShellsetHome");
            final Process pid = Runtime.getRuntime().exec(cmd,new String[] { "ORACLE_HOME=/oracle/app/oracle/product/11.2.0.4/dbhome/",
            "LD_LIBRARY_PATH=/usr/local/lib:/oracle/app/oracle/product/11.2.0.4/dbhome/lib:$LD_LIBRARY_PATH" });
            Process pid = Runtime.getRuntime().exec(cmd
            System.out.println("Process pid" + pid);
            bufferedReader = new BufferedReader(new InputStreamReader(pid.getErrorStream()), 1024);
            pid.waitFor();
            success = pid.exitValue();
            if(0==success){
                System.out.println("sqlldr success");
            }
            else  {
                String line = null;
                String errorMsg = "";
                while (bufferedReader != null && (line = bufferedReader.readLine()) != null) {
                    errorMsg += line;
                }
                throw new Exception("run" + shellCommand + "error" + errorMsg);
            }
            

        } catch (Exception ioe) {
            ioe.printStackTrace();
            System.out.println("Exception");
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            
        }
        return success;
    }

 

2. The remaining issues are related to permissions

$ORACLE_Permissions for home/RDBMS/MESG/oraus.msg and oraus.msb   ( It is also said that only one permission of oraus.msb needs to be changed (specific self-test is required)

It is also possible to put these two files in the upper directory, that is, $oracle_Home/RDBMS/and give sufficient permissions

If there is any mistake, please help correct it and discuss it together if there is a problem

Sudo starts Tomcat and reports an error. There is no Java environment

 Error:

Cannot find ./catalina.sh
The file is absent or does not have execute permission
This file is needed to run this program

Cause.

No executable permissions were granted to the files in the bin directory.

Solution:

chmod +x bin/*.sh

The error Neither the JAVA_HOME nor the JRE_HOME environment variable is defined.
The solution is as follows.

tomcat startup script file: startup.sh, startup.sh will load catalina.sh , catalina.sh will load setclasspath.sh

Linux: export JAVA_HOME=absolute path Windows: set

You can add directly to the second line.

#!/bin/sh
export JAVA_HOME=/usr/local/java1.8
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the “License”); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at

success

[[email protected] apache-tomcat-8.0.43]# sudo  bin/startup.sh
/home/report/apache-tomcat-8.0.43/bin/catalina.sh:行143: -Xmx1024m: 未找到命令
Using CATALINA_BASE: /home/report/apache-tomcat-8.0.43
Using CATALINA_HOME: /home/report/apache-tomcat-8.0.43
Using CATALINA_TMPDIR: /home/report/apache-tomcat-8.0.43/temp
Using JRE_HOME: /home/jdk-10.0.1
Using CLASSPATH: /home/report/apache-tomcat-8.0.43/bin/bootstrap.jar:/home/report/apache-tomcat-8.0.43/bin/tomcat-juli.jar
Tomcat started.

The above was added to the catalina.sh file. There is still the problem that the shutdown command runs with an error shutdown.sh

[[email protected] apache-tomcat-8.0.43]# sudo bin/shutdown.sh

/home/report/apache-tomcat-8.0.43/bin/catalina.sh: line 143: -Xmx1024m: Cannot find commands
Using CATALINA_BASE: /home/report/apache-tomcat-8.0.43
Using CATALINA_HOME: /home/report/apache-tomcat-8.0.43
Using CATALINA_TMPDIR: /home/report/apache-tomcat-8.0.43/temp
Using JRE_HOME: /home/jdk-10.0.1
Using CLASSPATH: /home/report/apache-tomcat-8.0.43/bin/bootstrap.jar:/home/report/apache-tomcat-8.0.43/bin/tomcat-juli.jar

./catalina.sh: 1: eval: usrjjdk-10.0.1re/bin/java: not found

It still says not found, and the path usrjdk-10.0.1jre is somehow missing the ‘/’.

Workaround.

rewrite the export JAVA_HOME environment variable you just modified to setclasspath.sh and remove the catalina.sh export one

Successfully solved:

[[email protected] apache-tomcat-8.0.43]# sudo  bin/shutdown.sh
/home/report/apache-tomcat-8.0.43/bin/catalina.sh:行143: -Xmx1024m: 未找到命令
Using CATALINA_BASE: /home/report/apache-tomcat-8.0.43
Using CATALINA_HOME: /home/report/apache-tomcat-8.0.43
Using CATALINA_TMPDIR: /home/report/apache-tomcat-8.0.43/temp
Using JRE_HOME: /home/jdk-10.0.1
Using CLASSPATH: /home/report/apache-tomcat-8.0.43/bin/bootstrap.jar:/home/report/apache-tomcat-8.0.43/bin/tomcat-juli.jar

 

 

[java development series] – Tomcat compilation error

There was a portable web project in eclipse before, but I found the problem when I logged in to the IDE again after I was useless for a long time

First of all, there are two versions of JDK in my computer, 1.6 and 1.7. Two versions of Tomcat 6 and 7 and two versions of eclipse ide 3.5 and 4.0 +

 

When I start the development environment and want to add an application to the server, I find an error:

There are no resources that can be added or removed from the server.

How can my previously easy-to-use application not work well. So I deleted the server and added it again. When I added Tomcat6, I found the following error:

Tomcat version 6.0 only supports j2ee 1.2 1.3 1.4 and java ee 5 web modules.

When I add 7, I find the following errors:

Project facet java version 1.7 is not supported

See the error report above, don’t think about it! It must be the version

 

It’s strange that 6 and 7 don’t work well. Search the Internet for information. The original project has a file: org. Eclipse. WST. Common. Project. Facet. Core. XML

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <fixed facet="wst.jsdt.web"/>
  <fixed facet="jst.web"/>
  <fixed facet="java"/>
  <installed facet="java" version="1.7"/>
  <installed facet="jst.web" version="3.0"/>
  <installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>

The JDK used for the compilation of the project and the version of jst.web are configured in this file. It can be found that I use 1.7 in the above configuration file, but my native JDK is 1.6

Therefore, change this to 1.6 test, and change the corresponding JST. Web to 2.5. The specific function of this configuration file needs to be studied under Tomcat. I can’t find the description of this configuration file. Send a blog and ask

Compiler error encountered in [java123] method overloading: both methods have same erasure = > introduce Java generic type erasure

Scene:

When the arguments of two overloaded functions are as follows

void func(Map< Integer, String> map) {}

void func(Map< Integer, List< String>> map) {}

IDE will report compilation errors: both methods have the same erasure

Answer:

Because Java generics erase the type at compile time, the above method becomes

void func(Map map)

After a query, there are several related concepts that need to be understood and mastered

https://www.jianshu.com/p/f9da328c91be

Java generics are different from C + + templates: Java generics are “type assurance” and C + + templates are “modified generic”

Type erasure: generic types exist only during compilation. The compiled bytecode and runtime do not contain generic information. All generic types are mapped to the same bytecode

Modified generic: generic types exist during compilation and running. The compiler automatically generates type code for each generic type and compiles it into binary code

The essence of type erasure

Generics (T) – > Compiler (type erasure) – > Original type (t replaced by object)

Generic (?)?extends XXX) –> Compiler (type erasure) – > Original type (t replaced by xxx)

Primitive type refers to the specific type of type variable in bytecode after the generic information is erased by compiler.

Type erasure leads to the limitation of generics

type erasure reduces the generalization of generics, which makes it impossible to use generic types in some important contexts.

overhead of implicit type conversion at runtime: when using generics, the java compiler automatically generates code for type conversion, which undoubtedly brings additional performance overhead compared with C + + templates

overload method signature conflict

a class cannot implement two variants of the same generic interface

https://blog.csdn.net/abc_ 12366/article/details/79177328

https://blog.csdn.net/weixin_ 34121282/article/details/88535522map generic

Error reported when Java connects mongodb com.mongodb.mongosocketopenexception: exception opening socket has been solved

Using idea to write java connection mongodb to report an error, let’s take a look at the detailed error information first

Solutions

Editor

vim mongodb/conf/mongod.conf

Add

bind_ip=0.0.0.0

After modification, the configuration file is as follows:

#Specify the start port
port=27017
#Specify the data storage directory
dbpath=data/db
#Specify the log storage directory
logpath=log/mongodb.log
#Run in the background
fork=true
#Log in with any IP
bind_ip=0.0.0.0

Causes of error information

Since mongodb version 3.6, it has_ The value of IP is localhost by default, so you can only use localhost to log in

Just change its IP to 0.0.0.0, you can use any IP login

Solution to the problem of Java runtime could not open….. Jvm.cfg

Website content quality is poor, distribution efficiency is too low how to do?Huawei engineers offer 5 unique skills>>>

solution of Java runtime could not open….. Jvm.cfg problem

Reference article:

(1) Solution to the problem of Java runtime could not open….. Jvm.cfg

(2) https://www.cnblogs.com/wth21-1314/p/6368059.html

Let’s make a note.

Is not an enclosing class appears in creating a Java class

Why can’t you stop buying 618?From the technical dimension to explore>>>

publicclassA{

publicclassB{

}

};

When you need to instance class B, according to the positive logic, A.B AB = new a.b()

Then the compiler will have an error — “is not an enclosing class”

Then look at the relevant java code, found that the original writing error! The right thing to do is

[java]

view plain

copy

Aa=newA();

A.Bab=a.newB();

a class in a class without static cannot be operated with an external class. An instance must be used to instantiate the class in the class

Solution to the problem of Java program “resource leak: ‘SC’ is never closed” in vscode

Recently I am learning java programming. The configuration of installation and environment variables will be described in detail in the following blog. This blog mainly introduces the warning of “Resource leak:’sc’ is never closed” in java input.

The test code is as follows.

import java.util.*;
public class Print {
    public static void main(String[] args) {
        double num=0;;
        Scanner sc=new Scanner(System.in);
        num = sc.nextDouble();
        if(num == 1){
            System.out.println("1");
        }
        else{
            System.out.println("0");
        }
    }
}

Warning: resource leak:’sc’ will never be closed.

This may bring some security problems, so how to solve this problem? Since’sc’ is not closed, then we only need to close’sc’. At this time, we need to add’sc.close();’ to the end to solve it.

The modified code is as follows.

import java.util.*;
public class Print {
    public static void main(String[] args) {
        double num=0;;
        Scanner sc=new Scanner(System.in);
        num = sc.nextDouble();
        sc.close();
        if(num == 1){
            System.out.println("1");
        }
        else{
            System.out.println("0");
        }
     }
 }