Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Using multiple classes and creating an Object of a class and calling its method in a different class

2801625May 15 2015 — edited May 21 2015

In the process of creating a new class, I need to move my main method from the class SaveDate to the class DynamicTest. Below I have listed the code of both classes.The objective is to be able to run my program from the DynamicTest Class. I need help understanding the process of moving my main method to a different class and creating an Object of a class and calling its method.

public class SaveData {

  private static final Map<String, Object> myCachedTreeMap = new TreeMap<String, Object>();

   public static final List<String> getLines(final String resourceParam, final Charset charset) throws IOException{

  System.out.println("Please get: "+resourceParam);

  if (myCachedTreeMap.containsKey(resourceParam) ) {

  // Use the cached file, to prevent an additional read.

  System.out.println("Found in memory : "+resourceParam);

  }

  else {

  // Load the file from disk

  System.out.println("Not Found in memory : "+resourceParam);

  }

  return null;

 

  }

  public static void main(String[] args) throws IOException {

  String target_dir = "C:\\myfiles\\config\\en";

  String output = "C:\\myfiles\\config\\en\\output.txt";

  File dir = new File(target_dir);

  File[] files = dir.listFiles();

  if (files == null || files.length < 1) {

  System.out.println("File list is empty...");

  return;

  }

  // open the Printwriter

  PrintWriter outputStream = new PrintWriter(output);

  try {

  for (File textFile : files) {

  if (textFile.isFile() && textFile.getName().endsWith(".txt")) {

  readFromDisk(textFile);

  }

  }

  }

  finally {

  outputStream.close();

  }

  String fileNameFromCache = "en_synonyms.txt";

  Object Sheet1 = myCachedTreeMap.get(fileNameFromCache);

  System.out.println(fileNameFromCache + " : \n" + Sheet1);

  }

  @SuppressWarnings("resource")

  private static void readFromDisk(File textFile) throws FileNotFoundException, IOException {

  BufferedReader inputStream;

  inputStream = null;

  String content = "";

  try {

  inputStream = new BufferedReader(new FileReader(textFile));

  content = readFile(textFile);

  System.out.println("Bytes Read = "+content.length());

  // Save contents

  FileContentsObject Sheet1 = new FileContentsObject(System.currentTimeMillis(),

  textFile.lastModified(), content,

  textFile.getName(),

  getLines(null, null));

  // add to map

  myCachedTreeMap.put(textFile.getName(), Sheet1);

  }

  finally {

  if (inputStream != null) {

  inputStream.close();

  }

  }

  }

  private static String readFile(File f) throws FileNotFoundException, IOException, UnsupportedEncodingException  {

  StringBuilder text = new StringBuilder(1024);

  int read, N = 1024 * 1024;

  char[] buffer = new char[N];

  BufferedReader br = null;

  try {

  br = new BufferedReader(

  new InputStreamReader(

                 new FileInputStream(f), "UTF8"));

  while(true) {

  read = br.read(buffer, 0, N);

  if (read > 0)

  text.append(new String(buffer, 0, read));

  if(read < N) {

  break;

  }

  }

  }

  finally {

  if (br != null)

  br.close();

  }

  return text.toString();

  }

  private static final class FileContentsObject {

  private long cachedTime; // currentTime

  private long lastModifiedTimestamp;

  private String contents;

  List<String> lines;

  private String fileName;

  public FileContentsObject(long cachedTime, long lastModifiedTimestamp,

  String contents, String fileName, List<String> lines) {

  this.cachedTime = cachedTime;

  this.lastModifiedTimestamp = lastModifiedTimestamp;

  this.contents = contents;

  this.fileName = fileName;

  this.lines = lines;

  SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm:ss");

  System.out.println("Current Time & Date:" + sdf.format(cachedTime));

  System.out.println("Last Modified Time Stamp:"

  + sdf.format(lastModifiedTimestamp));

  }

  /**

  *

  * @return The lines from the file

  */

  List<String> getLines() {

  return this.lines;

  }

  public String toString() {

  return "Sheet1{" + "fileName='" + fileName + '\'' + ", contents='"

  + contents + '\'' + ", lastModifiedTimestamp="

  + lastModifiedTimestamp + ", CurrentTime&Date="

  + cachedTime + '}';

 

  }

  }

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class DynamicTest {

  public static void main(String[] args) {

  Charset charset = Charset.forName("UTF-8");

  

  try {

  List<String> lines = CacheData.getLines("en_synonyms", charset) ;

  if (lines != null) {

  System.out.println("Number of Lines: "+lines.size());

  for (String line:lines) {

  System.out.println("DynamicTest:: "+line);

  }

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  

  try {

  List<String> lines = CacheData.getLines("en_stopwords", charset) ;

  if (lines != null) {

  System.out.println("Number of Lines: "+lines.size());

  for (String line:lines) {

  System.out.println("DynamicTest:: "+line);

  }

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

}

This post has been answered by unknown-7404 on May 15 2015
Jump to Answer

Comments

sb92075
4joey1 wrote:
Hallo!I am trying to install Oracle 11gR1 on Oracle Enterprise Linux 5.5.I have been trying to set DISPLAY parameters to launch the OUI but I get the error below.
If the OS environment is 100% correct, then you need to do NOTHING after login concerning
DISPLAY variable.

Two alternatives exist.
1) logon to DB Server directly via its "console"
2) Connect to DB Server from remote system using ssh -X or putty with X11 Forwarding enabled.

After login to DB Server & before you do anything else issue command below

env | sort

COPY command & results then PASTE all back here
Talip Hakan Ozturk
Can you try this one

# xhost +SI:localuser:oracle


Talip Hakan Ozturk
http://taliphakanozturken.wordpress.com/
Hans Forbrich
4joey1 wrote:
[root@joey-oel ~]# xhost +
access control disabled, clients can connect from any host
[root@joey-oel ~]# export DISPLAY=10.0.0.1:0.0
[root@joey-oel ~]# echo $DISPLAY
10.0.0.1:0.0
[root@joey-oel ~]# xclock
Error: Can't open display: 10.0.0.1:0.0
Is this a fresh terminal, or have you done any su to get to the root prompt. If any su, then this is no good. (Why are you at the root prompt anyway - baaaaad idea and baaaaad habit to get into if you are an Oracle DBA.)

Part 1 - on the XServer:

The XServer is the software that runs the screen. The XServer is NOT the software running the user program, such as dbca or runInstaller - that runInstaller is the XClient.

If you are using a Linux machine, and have gnome (KDE, twm, etc.), the userid you log in from the Linux splash screen is the one that owns the XServer.

The XServer needs permission to display things from the XClient. To give that permission, open a terminal from scratch - do not 'su' - and run 'xhost +' or preferrably 'xhost + {list of hosts with XClients}'

The XServer runs on a host, and operates on a DISPLAY and allows for multiple SCREENS.

To determine the DISPLAY and SCREEN, enter the command 'set | grep DISPLAY' to get something like "DISPLAY=:0.0"
To determine the host, enter 'hostname' to get something like 'bi11g.e2eo.ca'

The XClient will need this information to proceed.

So, at the top level, without having su'd, you enter 'xhost +' or 'xhost + bi11g.e2eo.ca'. (Stick with xhost +' for now.)

Part 2 - on the proposed XClient

Now you are ready to run the Oracle installer. You go to the machine that should run the installer by ssh, or telnet, or (if it's the local machine) by 'su - oracle'.

You want to tell the installer to send the display to the machine that has the XServer. You do that using "export DISPLAY={host}:{display info from the "set | grep DISPLAY" above}

IF IT ON THE LOCAL MACHINE AND YOU USED 'xhost +', THEN OMIT THE {host}!!! "export DISPLAY=:0.0"

and your session becomes:
[root@joey-oel ~]# xhost +
access control disabled, clients can connect from any host
*do NOT set or mess with DISPLAY - this is the server*
[root@joey-oel ~]# xclock
+and it displays+
[root@joey-oel ~]# su - oracle
[oracle@joey-oel ~]$ export DISPLAY=:0.0
*do NOT NOT NOT do another xhost here*
[oracle@joey-oel ~]$ xclock
+and it displays+
[oracle@joey-oel ~]$ cd /install/database
[oracle@joey-oel database]$ ./runInstaller
Starting Oracle Universal Installer...
806917
Talip and Hans - Where were you guys when I was struggling w this issue in late 2010? : ) Great comments and right on target.

4joey1 - I wrote up my experience, which I will paste below. This solution works well for me, and I no longer see $DISPLAY errors any longer.

------------------------------------------------------------------------------------------------------------------------------------------

Configuration
OS: Red Hat Enterprise Linux
Product: Oracle Universal Installer (oui) GUI

Problem
Many Oracle products, including the Oracle Database and WebLogic Server, use Oracle Universal Installer (oui) to install the products.

When starting the GUI oui within a vnc session or directly on the console from a terminal window, the following error appears:
$ ./runInstaller
Starting Oracle Universal Installer...
Checking Temp space: must be greater than 80 MB. Actual 35667 MB Passed
Checking swap space: must be greater than 150 MB. Actual 5951 MB Passed
Checking monitor: must be configured to display at least 256 colors
Could not execute auto check for display colors using command /usr/bin/xdpyinfo. Check if the DISPLAY variable is set. Failed <<<<
Some requirement checks failed. You must fulfill these requirements before
continuing with the installation,
Continue? (y/n) [n]
Ignoring required pre-requisite failures. Continuing...
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2011-06-01_02-45-14PM. Please wait ...
DISPLAY not set. Please set the DISPLAY and try again.

Issue
The error message focuses on $DISPLAY as the root cause. However, the problem is not $DISPLAY at all. The real issue is xhost.

Root cause
xhost authorizes OS users to connect to the X server. The OS user running the GUI oui has not been authorized by xhost, so oui fails.

To confirm that xhost is indeed the problem:
1) Open a terminal and sudo to root. Run xclock. xclock should display.
2) Open a terminal as the user running oui. Run xclock. xclock will fail to start.

Complications running the xhost command:
1) For OS users not previously authorized to run xhost, root has to run the xhost command.
2) Upon server reboot, xhost authorizations that root previously granted are lost.
3) Running the xhost command as root at system startup has no effect, because system startup occurs before the X server is started.

Solution
The OS users running the GUI oui should run the xhost command when the terminal session is started. To accomplish this:

root user
1) Copy set_xhost_current_user.sh into /usr/bin. Set permissions on the shell script to 755.
2) In the sudoers file:
a) Add /usr/bin/set_xhost_current_user.sh as a command alias.
b) Add the OS users who need to run oui to a group.
c) Assign the command alias to the group.

OS user running oui
1) Make sure $PATH contains /usr/bin.
2) In .bash_profile, call /usr/bin/set_xhost_current_user.sh.
3) Start a new terminal session and run GUI oui. oui should now start successfully.

set_xhost_current_user.sh
#!/bin/bash
# If user has logged in via gnome (e.g. vnc session), then
# run the xhost command. First validate the input parameter to
# prevent shell script command injection.

if [ "$COLORTERM" == "gnome-terminal" ] ; then

# Check the password file and compare to the parameter.
# If a match occurs, the parameter is the name of a valid user.
VALID_USER=`cat /etc/passwd | grep "/home" | cut -d: -f1 | grep $1`
if [ -z $VALID_USER ]; then
echo Invalid user name passed. xhost command not executed.
else
xhost + si:localuser:$1 > /dev/null
fi

fi

sudoers snippet
User_Alias DBA_USER_ALIAS2 = oraot1,oraot2

Cmnd_Alias SET_XHOST = /usr/bin/set_xhost_current_user.sh

DBA_USER_ALIAS2 t00539292 = NOPASSWD: SET_XHOST

bash profile snippet
sudo set_xhost_current_user.sh $USER
Hans Forbrich
Scot Krause wrote:
Talip and Hans - Where were you guys when I was struggling w this issue in late 2010? : ) Great comments and right on target.
I've posted my response on the forums many times over the years. Your search apparently did not work.
985229
The proble for me here i have posted the result after giving the env | sort cmd

env | sort
_=/bin/env
CVS_RSH=ssh
DISPLAY=:0.0
G_BROKEN_FILENAMES=1
HISTCONTROL=ignoredups
HISTSIZE=1000
HOME=/root
HOSTNAME=RedHatServer01
LANG=en_US.UTF-8
LESSOPEN=|/usr/bin/lesspipe.sh %s
LOGNAME=root
LS_COLORS=rs=0:
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=
SELINUX_USE_CURRENT_RANGE=
SHELL=/bin/bash
SHLVL=1
SSH_CLIENT=192.168.2.236 58774 22
SSH_CONNECTION=192.168.2.236 58774 192.168.2.40 22
SSH_TTY=/dev/pts/1
TERM=xterm
USER=root
XMODIFIERS=@im=none
EdStevens
982226 wrote:
The proble for me here i have posted the result after giving the env | sort cmd

env | sort
_=/bin/env
CVS_RSH=ssh
DISPLAY=:0.0
G_BROKEN_FILENAMES=1
HISTCONTROL=ignoredups
HISTSIZE=1000
HOME=/root
HOSTNAME=RedHatServer01
LANG=en_US.UTF-8
LESSOPEN=|/usr/bin/lesspipe.sh %s
LOGNAME=root
LS_COLORS=rs=0:
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=
SELINUX_USE_CURRENT_RANGE=
SHELL=/bin/bash
SHLVL=1
SSH_CLIENT=192.168.2.236 58774 22
SSH_CONNECTION=192.168.2.236 58774 192.168.2.40 22
SSH_TTY=/dev/pts/1
TERM=xterm
USER=root
XMODIFIERS=@im=none
ok, you just hijacked and resurrected a thread that is almost a year old. And given no explanation of your problem, which may or may not be the same as the problem being addressed in this thread when it was active last year.

Please start your own thread, describing your own problem and symptoms. Be sure to state your own operating system and vesions of Oracle.

And please read the forum FAQ. The link is in the upper right corner of this very page.

Moderator: please lock this thread.

Edited by: EdStevens on Jan 16, 2013 8:53 AM
1 - 7
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 18 2015
Added on May 15 2015
3 comments
1,188 views