错误:找不到符号 - 当它依赖于子文件夹中的类时,如何让我的类编译?

时间:2017-01-15 02:38:57

标签: java json compiler-errors

在我的包alignmentparser中,我有一个名为Subject的类,其中我有一个我想要转换为JSON对象的字符串。在研究了如何实现这一目标之后,我找到了包JSON-java,我将其下载并作为名为JSON的子目录放入我的包中。

我的类的包语句状态为package alignmentparser;。我将JSON子目录中每个文件中的package语句更新为package alignmentparser.JSON;

现在我无法编译我的TestRunner类(依赖于Subject)。我收到这些错误消息:

./alignmentparser/Subject.java:85: error: cannot find symbol
JSONObject json = new JSONObject(sb.toString());
      ^   symbol:   class JSONObject   location: class Subject    
./alignmentparser/Subject.java:85: error: cannot find symbol
JSONObject json = new JSONObject(sb.toString());
      ^   symbol:   class JSONObject   location: class Subject

如何通过这些错误消息?

编辑:这是整个代码,以防万一我不知道有错误。

Subject.java

package alignmentparser;

import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.util.regex.*;

public class Subject {
  private String id;
  private String sequence;
  // Position on subject where alignment with query begins, inclusive 
  private int start;
  // Position on subject where alignment with query ends, inclusive
  private int end;

  public Subject(String accessNum, int hitFrom, int hitTo, String seq) {
    id = accessNum;
    sequence = seq;
    start = hitFrom;
    end = hitTo;
    getEntireSubject();
  }

  // Getters
  public String getSequence() {
    return sequence;
  }
  public int getStart() {
    return start;
  }
  public int getEnd() {
    return end;
  }

  // Fetches accession number from NCBI
  // https://www.ncbi.nlm.nih.gov/books/NBK25498/#chapter3.Application_2_Converting_access
  private void getEntireSubject() {
    try {
      // e post to Entrez
      String link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/epost.fcgi?db=nucleotide&id=" + id;
      URLConnection connection = new URL(link).openConnection();
      connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
      connection.connect();
      BufferedReader r  = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

      String webEnv = "";
      String queryKey = "";
      // Put together string to be scanned to find patterns
      StringBuilder sb = new StringBuilder();
      String line;
      while ((line = r.readLine()) != null) {
        sb.append(line);
      }
      String result = sb.toString();
      // Specify patterns to search for
      String queryKeyRegEx = "<QueryKey>(\\d+)<\\/QueryKey>";
      String webEnvRegEx = "<WebEnv>(\\S+)<\\/WebEnv>";
      // Create Pattern objects
      Pattern queryKeyPattern = Pattern.compile(queryKeyRegEx);
      Pattern webEnvPattern = Pattern.compile(webEnvRegEx);
      // Create Matcher objects
      Matcher queryKeyMatcher = queryKeyPattern.matcher(result);
      if (queryKeyMatcher.find()) {
        queryKey = queryKeyMatcher.group(1);
        System.out.println(queryKey);
      }
      Matcher webEnvMatcher = webEnvPattern.matcher(result);
      if (webEnvMatcher.find()) {
        webEnv = webEnvMatcher.group(1);
        System.out.println(webEnv);
      }

      // e fetch to Entrez
      link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&query_key="+queryKey+"&WebEnv="+webEnv+"&usehistory=y";
      connection = new URL(link).openConnection();
      connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
      connection.connect();
      r  = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

      sb = new StringBuilder();
      while ((line = r.readLine()) != null) {
        sb.append(line);
      }
      JSONObject json = new JSONObject(sb.toString());
      System.out.println(json);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

TestRunner.java

package alignmentparser;

import java.util.*;

class TestRunner {
  private String query = "ATCCTGTCTGTGTTAGGAG-AGTCTACTTC-TTAACNGAGGGATTCANTNTTTCCTGCANAGGCGGCCGTCNATGAANACCCTGTTTGTGGACAGCTACNGNGAGATGCTTTTNTTTCTGCAGTCACTGTTCATGCTGGCCACCGTGGTGCTGTACTTCANCCACCTCAAGGAGTATGTGGCTTCCATGGTATTCTCCCTGGCCTTGGGCTGGNCCAACATGCTCTACTACNCCCGCGGTTTCCAGCAGANGGGCATCTATGCCGTCATGATANANAANATGATCCTGAGAGACCTGTGCCGTTTCATGTTTGTCTACGTCGTCTTNTTGTTCGGNTTTTCCACAGCGGNGGTGACGCTGATTGAAGACGGNAANAATGACTCCCTGCCGTCTGAGTCCACGTCGCACAGGNGGNGGGGGCCTGCCTGCANGCCCCCCGATAGCTCCTACAACAGCCTGTACTCCACCTGCCTGGAGCTGTTCAAGTTCACCATCGGCATGGGCGACCTGGANTTCACTGANAACTATGACTTCAAGGCTGTCTTCATCATCCTGCTGCTGGCCTATGTAATTCTCACCTACATCCTCNTGCTCAACATGCTNATCGCCCTCNTGGGTGAGACTGTCAACAAGATCGCACAGNNAGAGCAAGAACATCTGGAANCTGCAGAGAGCCATCACCATCCTGNACACGGAGAAGAGCTTCCTTAAGTGCATGAGGAAGGCCTTCCGCTCAGGCAAGCTGCNTGCANGTGGGGTACACACCTGATGGCAAGGACGACTACCGG";
  private String midline = "||||||||||||||||||| |||||||||| ||  | |||||||||| | ||||||||| ||||||||||| ||||| ||||||||||||||||||||| | ||||||||||| |||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||| |||||||||||||||||| |||||||||||||||||||||| | || ||||||||||||||||||||||||||||||||||||||| ||||||| |||||||| ||||||||||||| ||||||||||||||||||||| || |||||||||||||||||||||||||||||||||||| || ||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||| ||||||||| |||||||||||||||||||||||||||||  ||||||||||||||||||| |||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||| ||||||||||||||||||||||||||||||||||||";
  private Subject subj = new Subject("NM_080704", 1710, 2495, "ATCCTGTCTGTGTTAGGAGGAGTCTACTTCTTTTTCCGAGGGATTCAGTATTTCCTGCAGAGGCGGCCGTCGATGAAGACCCTGTTTGTGGACAGCTACAGTGAGATGCTTTTCTTTCTGCAGTCACTGTTCATGCTGGCCACCGTGGTGCTGTACTTCAGCCACCTCAAGGAGTATGTGGCTTCCATGGTATTCTCCCTGGCCTTGGGCTGGACCAACATGCTCTACTACACCCGCGGTTTCCAGCAGATGGGCATCTATGCCGTCATGATAGAGAAGATGATCCTGAGAGACCTGTGCCGTTTCATGTTTGTCTACATCGTCTTCTTGTTCGGGTTTTCCACAGCGGTGGTGACGCTGATTGAAGACGGGAAGAATGACTCCCTGCCGTCTGAGTCCACGTCGCACAGGTGGCGGGGGCCTGCCTGCAGGCCCCCCGATAGCTCCTACAACAGCCTGTACTCCACCTGCCTGGAGCTGTTCAAGTTCACCATCGGCATGGGCGACCTGGAGTTCACTGAGAACTATGACTTCAAGGCTGTCTTCATCATCCTGCTGCTGGCCTATGTAATTCTCACCTACATCCTCCTGCTCAACATGCTCATCGCCCTCATGGGTGAGACTGTCAACAAGATCGCACAGG-AGAGCAAGAACATCTGGAAGCTGCAGAGAGCCATCACCATCCTGGACACGGAGAAGAGCTTCCTTAAGTGCATGAGGAAGGCCTTCCGCTCAGGCAAGCTGC-TGCAGGTGGGGTACACACCTGATGGCAAGGACGACTACCGG");

  public static void main(String[] args) {
    TestRunner analyzer = new TestRunner();
    analyzer.identifyMismatches();
    analyzer.identifyResidue(547);
  }

  /**
  * Identifies the residue encoded by the codon at the specified codon count.
  */
  private void identifyResidue(int pos) {
    HashMap<String, ArrayList<String>> codons = assembleCodonMap();

    // Set base position of first codon in the CDS (to be retrieved from GenBank later)
    int posOfCDS = 276;
    // Adjust given codon count to account for any difference between CDS / query length
    // NOTE: Subtract 3 to account for conversion between codon count and base count
    int adjustedPos = (pos * 3) - (subj.getStart() - posOfCDS) - 3;
    System.out.println(adjustedPos + subj.getStart());

    if (adjustedPos < 0) {
      System.out.println("Codon " + pos + " is not viewable in this alignment.");
      //System.out.println("You can view codons " + (posFirstCodon + 1) + " through " + ((2495 - 276) / 3));
    } 

    // Get codon
    String found = "";
    for (int i = 0; i < query.length(); i += 3) {
      if (i == adjustedPos) {
        found = query.substring(i, i + 3);
        System.out.println(found + " at " + (i + subj.getStart()));
      }
    }

    // Identify residue that codon translates to
    for (String codon : codons.keySet()) {
      if (codons.get(codon).contains(found)) {
        System.out.println("Residue is " + codon);
      }
    }
  }


  private HashMap<String, ArrayList<String>> assembleCodonMap() {
    ArrayList<String> ala = new ArrayList<String>();
    ala.add("GCT");
    ala.add("GCC");
    ala.add("GCA");
    ala.add("GCG");
    ArrayList<String> arg = new ArrayList<String>();
    arg.add("CGT");
    arg.add("CGC");
    arg.add("CGA");
    arg.add("CGG");
    arg.add("AGA");
    arg.add("AGG");
    ArrayList<String> asn = new ArrayList<String>();
    asn.add("AAT");
    asn.add("AAC");
    ArrayList<String> asp = new ArrayList<String>();
    asp.add("GAT");
    asp.add("GAC");
    ArrayList<String> cys = new ArrayList<String>();
    cys.add("TGT");
    cys.add("TGC");
    ArrayList<String> gln = new ArrayList<String>();
    gln.add("CAA");
    gln.add("CAG");
    ArrayList<String> glu = new ArrayList<String>();
    glu.add("GAA");
    glu.add("GAG");
    ArrayList<String> gly = new ArrayList<String>();
    gly.add("GGT");
    gly.add("GGC");
    gly.add("GGA");
    gly.add("GGG");
    ArrayList<String> his = new ArrayList<String>();
    his.add("CAT");
    his.add("CAC");
    ArrayList<String> ile = new ArrayList<String>();
    ile.add("ATT");
    ile.add("ATC");
    ile.add("ATA");
    ArrayList<String> leu = new ArrayList<String>();
    leu.add("TTA");
    leu.add("TTG");
    leu.add("CTT");
    leu.add("CTC");
    leu.add("CTA");
    leu.add("CTG");
    ArrayList<String> lys = new ArrayList<String>();
    lys.add("AAA");
    lys.add("AAG");
    ArrayList<String> met = new ArrayList<String>();
    met.add("ATG");
    ArrayList<String> phe = new ArrayList<String>();
    phe.add("TTT");
    phe.add("TTC");
    ArrayList<String> pro = new ArrayList<String>();
    pro.add("CCT");
    pro.add("CCC");
    pro.add("CCA");
    pro.add("CCG");
    ArrayList<String> ser = new ArrayList<String>();
    ser.add("TCT");
    ser.add("TCC");
    ser.add("TCA");
    ser.add("TCG");
    ser.add("AGT");
    ser.add("AGC");
    ArrayList<String> thr = new ArrayList<String>();
    thr.add("ACT");
    thr.add("ACC");
    thr.add("ACA");
    thr.add("ACG");
    ArrayList<String> trp = new ArrayList<String>();
    trp.add("TGG");
    ArrayList<String> tyr = new ArrayList<String>();
    tyr.add("TAT");
    tyr.add("TAC");
    ArrayList<String> val = new ArrayList<String>();
    val.add("GTT");
    val.add("GTC");
    val.add("GTA");
    val.add("GTG");

    HashMap<String, ArrayList<String>> codons = new HashMap<String, ArrayList<String>>();
    codons.put("alanine", ala);
    codons.put("arginine", arg);
    codons.put("asparagine", asn);
    codons.put("aspartic acid", asp);
    codons.put("cysteine", cys);
    codons.put("glutamine", gln);
    codons.put("glutamic acid", glu);
    codons.put("glycine", gly);
    codons.put("histidine", his);
    codons.put("isoleucine", ile);
    codons.put("leucine", leu);
    codons.put("lysine", lys);
    codons.put("methionine", met);
    codons.put("phenylalanine", phe);
    codons.put("proline", pro);
    codons.put("serine", ser);
    codons.put("threonine", thr);
    codons.put("tryptophan", trp);
    codons.put("tyrosine", tyr);
    codons.put("valine", val);

    return codons;
  }


  /**
  * Prints out mismatches in the alignment of subject and query. 
  * Base positions are referenced from subject.
  */
  private void identifyMismatches() {
    // Loops through each character in the midline
    for (int i = 0; i < midline.length(); i++) {
      // If character is a space, denoting a mismatch
      if (midline.charAt(i) == ' ' && query.charAt(i) != 'N') {
        // Print out the characters at the same position in subject & query
        System.out.println(subj.getStart() + i);
        System.out.println(subj.getSequence().charAt(i) + ">" + query.charAt(i));
      }
    }
  } 
}

编辑2:我的文件夹结构的屏幕截图。

enter image description here

1 个答案:

答案 0 :(得分:1)

首先,除非您计划修改JSON-java的源代码,否则您并不需要将项目作为源代码包含在内。只需修改项目依赖项以包含JSON-java jar工件或将其包含在类路径中,以便在运行时找到必要的类。

其次,如果您真的想将项目作为源代码包含并自行编译,那么您仍然不需要更改项目的包结构。您可以编译它并在应用程序之外构建工件(jar)。

第三,如果您仍希望将项目作为源代码包含在自己的包中,则需要修改此项目中每个类的包声明。看起来你已经尝试过了。但是为了确保你有,例如将类JSONObject的包名从org.json更改为alignmentparser.org.json?此外,当您想在自己的代码中使用这些JSON-java类中的一个或多个时,需要导入这些类。例如。 import org.json.JSONObject。我在你的代码示例(Subject.java)中看到,你没有这样做。一旦修复了包重命名并包含必要的类import语句,就不会出现任何编译器错误。